通过jest进行反应测试会产生意外错误

时间:2017-12-03 13:54:57

标签: reactjs unit-testing jestjs babel-jest

我正在使用JEST来测试我的反应应用。

我收到错误和一些文字,如下图所示。

enter image description here

此外,我的测试用例(名为:TodoApp.test.jsx )的代码如下:

it("should add todo ...", () => {
    // const text = "Suzal is trying react";

    // I commented out the other lines because the test
    // only gave error when this line was included.

    const todoApp = TestUtils.renderIntoDocument(<TodoApp />);

    // todoApp.state = {
    //     todos: []
    // }
    // todoApp.handleAddTodo(text);
    // expect(todoApp.state.todos[0].text).toBe(text);
});
  

如果需要额外的代码/说明,请询问。完整的文件在Github上:TodoApp.test.jsx

我已经完成的链接

1 个答案:

答案 0 :(得分:0)

我克隆了你的回购并开始评论事情,并发现在设置初始状态时,构造函数中TodoApp.jsx第15行的TodoApi.getTodos()似乎正在发生一些事情。如果删除对TodoApi.getTodos的调用并使用空数组替换,则测试错误将消失。您可能需要为TodoApi.getTodos创建一个模拟函数,以使测试通过。

https://facebook.github.io/jest/docs/en/mock-functions.html