如何使用Jest在React JS中使用Mock函数?

时间:2018-04-14 05:28:14

标签: reactjs unit-testing react-router react-redux jestjs

我试图使用:Facebook's Jest Mock Functions

模拟一个函数

然而,我收到此错误:

 jest.mock is not a function

    TypeError: jest.mock is not a function
        at evaluate (https://oq7kwzrnj5.codesandbox.io/src/ListItem.spec.js:21:6)
        at r.a (https://codesandbox.io/static/js/sandbox.f3fc5e5b.js:5354:971)
        at e.value (https://codesandbox.io/static/js/sandbox.f3fc5e5b.js:5413:270)
        at e.value (https://codesandbox.io/static/js/sandbox.f3fc5e5b.js:5386:23195)
        at e.value (https://codesandbox.io/static/js/sandbox.f3fc5e5b.js:5386:22974)
        at e. (https://codesandbox.io/static/js/sandbox.f3fc5e5b.js:5386:18687)
        at n (https://codesandbox.io/static/js/common-sandbox.5c75aa32.js:2847:811)
        at Generator._invoke (https://codesandbox.io/static/js/common-sandbox.5c75aa32.js:2847:599)
        at Generator.e.(anonymous function) [as next] (https://codesandbox.io/static/js/common-sandbox.5c75aa32.js:2847:990)
        at o (https://codesandbox.io/static/js/common-sandbox.5c75aa32.js:3049:316)
        at https://codesandbox.io/static/js/common-sandbox.5c75aa32.js:3049:464
        at new Promise ()
        at new t (https://codesandbox.io/static/js/common-sandbox.5c75aa32.js:678:776)
        at https://codesandbox.io/static/js/common-sandbox.5c75aa32.js:3049:258
        at https://codesandbox.io/static/js/sandbox.f3fc5e5b.js:5386:18996
        at Array.map ()

这是我的代码: https://codesandbox.io/s/oq7kwzrnj5

有人可以帮我解决这个错误吗?

任何更新

1 个答案:

答案 0 :(得分:0)

你可以像这样模拟你的功能 mockMyFunc = jest.fn()

同时查看我对这个问题的回答: why actual and expected length is not equal in react js