javascript - 开 Jest - 测试 mobx store @action TypeError : is not a function

标签 javascript ecmascript-6 jestjs mobx

我有以下商店:

import { observable, action } from "mobx";

class UiStore {
  @observable string;

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

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

export default UiStore;

我 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");
  });
});

但是我收到以下错误:

TypeError: uiStore.setString is not a function

当我删除 @action 装饰器时,测试将通过。但是根据mobx docs ,当函数修改状态时,@action 装饰器显着提高了性能。有人知道测试 mobx @actions 的方法吗?

最佳答案

不使用箭头函数解决了这个问题:

import { observable, action } from "mobx";

class UiStore {
  @observable string;

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

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

export default UiStore;

不知道为什么...

关于javascript - 开 Jest - 测试 mobx store @action TypeError : is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46621580/

相关文章:

javascript - 如何使用 jQuery 将一个元素移动到另一个元素中

Javascript - 结合还是不结合,这是个问题

javascript - 单击以使用 Javascript/ES6 更改内部子项和其他部分

javascript - Node 8.9.4 还需要 babel 吗?

javascript - 在使用 jest 的 vue 测试中找不到模块 '@jest/globals'

javascript - 如何使用 jQuery 获取表 tds 的类名并将其写入数组?

javascript - 将我的 'IDs' 动态加载到我的 Backbone Collection 中?

javascript - Angular 4 - 检测到循环依赖。两个组件导入了另一个组件的 .ts 文件

reactjs - Jest 快照在本地工作,但在 Travis CI 上始终失败

node.js - Jest的spyOn方法不会被调用,但是一旦改变测试用例顺序就会被调用