Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 在es6中用jest进行单元测试大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在反应世界很新,试图写简单的friendslist应用程序.我以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测试执行测试时,我有错误信息:

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

Error: no test specified

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

解决方法

要测试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安装

大佬总结

以上是大佬教程为你收集整理的node.js – 在es6中用jest进行单元测试全部内容,希望文章能够帮你解决node.js – 在es6中用jest进行单元测试所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。