大佬教程收集整理的这篇文章主要介绍了node.js – 在es6中用jest进行单元测试,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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([]); }); });
> react-starterify@0.0.9 test /Volumes/Developer/reactjs/app5 > echo "Error: no test specified" Error: no test specified
我究竟做错了什么?文件结构如下所示:
对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,请注明来意。