在es6中用jest进行单元测试

zer*_*ing 12 unit-testing node.js reactjs

我在反应世界中很新,并试图编写简单的朋友列表应用程序.我用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;
Run Code Online (Sandbox Code Playgroud)

现在我想测试我的商店并在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([]);
    });



}); 
Run Code Online (Sandbox Code Playgroud)

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

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

Error: no test specified
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?文件结构如下: 在此输入图像描述

Jar*_*tra 2

要测试 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"]
  }
}
Run Code Online (Sandbox Code Playgroud)

并运行:

$ npm install

  • 这应该更新为仅在 "jest": { "transform": {".*": "&lt;rootDir&gt;/node_modules/babel-jest"} } 下添加。“scriptPreprocessor”已弃用。 (2认同)