【问题标题】:How to use Mock Functions in React JS using Jest?如何使用 Jest 在 React JS 中使用模拟函数?
【发布时间】:2018-04-16 08:05:21
【问题描述】:

我正在尝试使用以下方法模拟一个函数: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

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

任何更新

【问题讨论】:

  • 你能把你的代码贴在一些仓库里,这样我就可以在本地运行它了吗?
  • @TomaszMularczyk 它在场codesandbox.io/s/oq7kwzrnj5
  • @TomaszMularczyk 从早上开始我尝试了不同的方法但没有成功这是我更新的代码codesandbox.io/s/oq7kwzrnj5
  • @user5711656 可能是codesandbox 的原因,它会阻止代码正常运行。这就是为什么让您的项目在本地运行会很有帮助的原因。

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2019-07-22
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 2021-08-11
    • 2020-09-16
    • 1970-01-01
    相关资源
    最近更新 更多