【发布时间】: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