【问题标题】:React unit test with jest in es6在 es6 中用 jest 反应单元测试
【发布时间】:2015-07-10 15:11:04
【问题描述】:

我是 React 世界的新手,正在尝试编写简单的好友列表应用程序。我用 es6 风格编写了我的朋友商店,并使用 babel 作为从 es5 到 es6 的转换器。

import AppDispatcher from '../dispatcher/app_dispatcher';
import { EventEmitter } from 'events';
import FRIENDS_CONST from '../constants/friends';

const CHANGE_EVENT = 'CHANGE';

let friendsList = [];


let add = (name) => {

    let counter = friendsList.length + 1;
    let newFriend = {
        id: counter,
        name: name
    };

    friendsList.push(newFriend);

}

let remove = (id) => {
    let index = friendsList.findIndex(e => e.id == id);
    delete friendsList[index];
}


let FriendsStore = Object.assign({}, EventEmitter.prototype, {

    getAll: () => {
        return friendsList;
    },

    emitChange: () => {
        this.emit(CHANGE_EVENT);
    },

    addChangeListener: (callback) => {
        this.on(CHANGE_EVENT, callback);
    },

    removeChangeListener: (callback) => {
        this.removeListener(CHANGE_EVENT, callback);
    }

});

AppDispatcher.register((action) => {

    switch (action.actionType) {
        case FRIENDS_CONST.ADD_FRIENDS:
            add(action.name);
            FriendsStore.emitChange();
            break;
        case FRIENDS_CONST.REMOVE_FRIENDS:
            remove(action.id);
            FriendsStore.emitChange();
            break;
    }

});

export default FriendsStore;

现在我想测试我的商店并在 es6 中编写单元测试

jest.dontMock('../../constants/friends');
jest.dontMock('../friends_store');


describe('FriendsStore', () => {

    import FRIENDS from '../../constants/friends';
    import AppDispatcher from '../../dispatcher/AppDispatcher';
    import FriendsStore from '../friends_store';

    let FakeAppDispatcher;
    let FakeFriendsStore;
    let callback;

    let addFriends = {
        actionType: FRIENDS.ADD_FRIENDS,
        name: 'Many'
    };

    let removeFriend = {
        actionType: FRIENDS.REMOVE_FRIENDS,
        id: '3'
    };

    beforeEach(function() {
        FakeAppDispatcher = AppDispatcher;
        FakeFriendsStore = FriendsStore;
        callback = AppDispatcher.register.mock.calls[0][0];
    });

    it('Should initialize with no friends items', function() {
        var all = FriendsStore.getAll();
        expect(all).toEqual([]);
    });



}); 

当我使用语句 npm test 执行测试时,我收到了错误消息:

> react-starterify@0.0.9 test /Volumes/Developer/reactjs/app5
> echo "Error: no test specified"

Error: no test specified

我做错了什么?文件结构如下所示:

【问题讨论】:

  • 我不熟悉 react-starterify 正在做什么,但您可以查看 babel-jest,它会期望您的测试也在 es6 中。
  • 现在仔细阅读 jest repo,你会发现一些与 ES6、Babel 和更新版本的 node (>0.10.x) 相关的未解决问题。现在一切似乎都很糟糕,给他们几个月的时间来赶上:S
  • 我发现拼凑一个 karma+jasmine 系统比使用 Jest 更容易。
  • babel-jest 已移至 Jest 存储库。源代码可以在 packages/babel-jest 中找到。 babel-jest 现在由 Jest 自动加载并完全集成。 source

标签: node.js unit-testing reactjs


【解决方案1】:

要测试 ES6 语法和 JSX 文件,需要将它们转换为 Jest。 Jest 有一个配置变量,您可以在其中定义预处理器(scriptPreprocessor)。您可以使用babel-jest 预处理器:

对 package.json 进行以下更改:

{
  "devDependencies": {
    "babel-jest": "*",
    "jest-cli": "*"
  },
  "scripts": {
    "test": "jest"
  },
  "jest": {
    "scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
    "testFileExtensions": ["es6", "js"],
    "moduleFileExtensions": ["js", "json", "es6"]
  }
}

然后运行:

$ npm install

【讨论】:

【解决方案2】:

我是按照教程做的:

npm install --save-dev jest babel-jest babel-preset-es2015 babel-preset-react react-test-renderer

然后

添加到 package.json

  "scripts": {
    "test": "jest"
  },
  "jest": {
    "testPathDirs": [
      "src/main/resources/web_pages/__tests__"
    ]
  },

结果:

 PASS  src/main/resources/web_pages/__tests__/modules/utils/ValidationUtil.spec.js (5.214s)
  ✓ ValidateEmail (5ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        6.092s, estimated 7s
Ran all test suites.

【讨论】:

    猜你喜欢
    • 2016-06-27
    • 2020-08-27
    • 2020-03-02
    • 2018-12-28
    • 1970-01-01
    • 2019-11-20
    • 2019-05-28
    • 2018-09-14
    • 1970-01-01
    相关资源
    最近更新 更多