jest - testing mobx store @action TypeError: is not a function

Viewed 3158

I have the following store:

import { observable, action } from "mobx";

class UiStore {
  @observable string;

  constructor() {
    this.string = "My string";
  }

  @action
  setString = value => {
    this.string = value;
  };
}

export default UiStore;

And a simple test I am running with jest:

import UiStore from "./UiStore";

describe("uiStore", () => {
  it("setString sets string to the passed value", () => {
    const uiStore = new UiStore();
    uiStore.setString("blah");
    expect(uiStore.string).toBe("blah");
  });
});

But I am getting the following error:

TypeError: uiStore.setString is not a function

When I remove the @action decorator, the test will pass. But according to the mobx docs, the @action decorator significantly increases performance when the functions are modifying state. Does anybody know of a way to test mobx @actions?

2 Answers

Not using a arrow function solves this problem:

import { observable, action } from "mobx";

class UiStore {
  @observable string;

  constructor() {
    this.string = "My string";
  }

  @action
  setString(value) {
    this.string = value;
  }
}

export default UiStore;

No idea why though...

Related