reactjs - jest.mock(module) 和 jest.fn() 有什么区别

标签 reactjs jestjs testunit enzyme

我尝试了几种不同的方法来定义模拟函数,但所有尝试都失败了。当我尝试将其定义如下时:

jest.mock('../src/data/server', ()=> ({server: {report: jest.fn()}}));
expect(server.report.mock).toBeCalledWith(id, data, () => {...}, () => {...});

我收到此错误:

expect(jest.fn())[.not].toBeCalledWith()
jest.fn() value must be a mock function or spy.
Received: undefined

如果我将模拟定义为:

var spy = jest.mock('../src/data/server', ()=> ({server: {report: jest.fn()}}));
expect(spy).toBeCalledWith(id, data, () => {...}, () => {...});

它返回以下错误:

    expect(jest.fn())[.not].toBeCalledWith()
    jest.fn() value must be a mock function or spy.
    Received:
      object: {"addMatchers": [Function anonymous], "autoMockOff": [Function anonymous], "autoMockOn": [Function anonymous], "clearAllMocks": [Function anonymous], "clearAllTimers": [Function anonymous], "deepUnmock": [Function anonymous], "disableAutomock": [Function anonymous], "doMock": [Function anonymous], "dontMock": [Function anonymous], "enableAutomock": [Function anonymous], "fn": [Function anonymous], "genMockFn": [Function bound getMockFunction], "genMockFromModule": [Function anonymous], "genMockFunction": [Function bound getMockFunction], "isMockFunction": [Function isMockFunction],
 "mock": [Function anonymous], "resetModuleRegistry": [Function anonymous], "resetModules": [Function anonymous], "runAllImmediates": [Function anonymous], "runAllTicks": [Function anonymous], "runAllTimers": [Function anonymous], "runOnlyPendingTimers": [Function anonymous], "runTimersToTime": [Function anonymous],"setMock": [Function anonymous], "unmock": [Function anonymous], "useFakeTimers": [Function anonymous], "useRealTimers": [Function anonymous]}

作为我的第三次尝试,我将模拟定义为:

const st = require.requireActual('../src/data/server', ()=> ({server: {report: jest.fn()}}));
st.report = jest.fn();
expect(st.report).toBeCalledWith(id, data,  () => {...}, () => {...});

我收到此错误:

expect(jest.fn()).toBeCalledWith(expected)
Expected mock function to have been called with:
  ["1033083fe", {"address": "test address", "affiliation": "testaaa", 
  "city": "testcity", "copyright": true, "country": "testcountry", "email": "sss@test.com", "message": "testmessage", 
  "name": "testname", "phone": "1234567890", "zipcode": "12345"}, [Function anonymous], [Function anonymous]]
But it was not called.

我想知道问题是什么以及这三种定义模拟的方法有何不同?

附注代码可以在这里找到:Write a Unit test in Jest for a React form

最佳答案

在第一个示例中,您说当模块 '../src/data/server' 被导入到其他模块中时,它将是 {server: {report: jest .fn()}}。在下一行中,您尝试使用服务器。问题是你永远不会导入你模拟的模块。也许在您测试 server 的模块中是 {server: {report: jest.fn()}},但在您的测试服务器中只是 undefined 因为你从不导入它。要解决此问题还必须导入它。

import server from '../src/data/server'
jest.mock('../src/data/server', ()=> ({server: {report: jest.fn()}}));
expect(server.report.mock).toBeCalledWith(id, data, () => {...}, () => {...});

在第二个示例中,它看起来像 jest.mock 只是返回 jest

最后一个示例失败,因为您从未调用 st.report

回到主要问题。 jest.mockjest.fn

之间的区别

jest.mock 将一个模块替换为 jest.fn,当您仅使用路径参数调用它时,或者使用函数的返回值来调用它,您可以将其作为第二个参数。因此,在您的第一个示例中,当在您想要测试的文件或测试本身中时,导入 '../src/data/server' ,它将是 {server: {报告:jest.fn()}}.

这给我们带来了jest.fn()。这将返回一个 spy 。 spy 是一个可以记住对其进行的每个调用以及使用哪些参数的函数。

假设您有一个想要测试的模块:

import server from './data/server'

server.report('test123')

要测试此调用是否使用正确的参数进行,您需要使用您可以控制的内容模拟对 './data/server' 的依赖关系。

import server from '../src/data/server'
import fileToTest from '../src/fileToTest'
jest.mock('../src/data/server', ()=> ({server: {report: jest.fn()}}));
expect(server.report.mock).toBeCalledWith('test123');

这里发生的事情是,在所有导入内容开始之前,jest 将 '../src/data/server' 替换为您的模拟实现,因此当 fileToTest 导入时服务器并调用它,它实际上调用了 spy 功能。然后您可以预期它是使用正确的参数调用的。

顺便说一句。您在测试中尝试的检查功能将不起作用,因为您在调用 spy 时传递的函数与您传递给 toBeCalledWith 的函数不相同。

在这种情况下,我将检查每个参数

expect(server.report.mock.calls[0][0]).toBe("1033083fe");
expect(server.report.mock.calls[0][0]).toEqual({"address": "test address", "affiliation": "testaaa", 
  "city": "testcity", "copyright": true, "country": "testcountry", "email": "sss@test.com", "message": "testmessage", 
  "name": "testname", "phone": "1234567890", "zipcode": "12345"});

关于reactjs - jest.mock(module) 和 jest.fn() 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41089214/

相关文章:

ruby - 我如何模拟或覆盖 Kernel.system?

ruby - 在使用 TestRunner 运行之前将参数/对象传递给 ruby​​ 单元/测试

ruby-on-rails - Rails 组织单元测试的方式是什么?

javascript - 在我的 nodejs 测试中自动化用户输入

node.js - testcafe 通过 intelliJ run 操作执行

javascript - React Material UI - 清除字段按钮后自动完成组件按钮

reactjs - 使用react-bootstrap模式时无法读取未定义的属性 'Header'

javascript - 为什么箭头语法优先于功能性 React 组件的函数声明?

javascript - 为什么从我的 reducer 返回的状态是一个空对象??(React-Redux)

reactjs - Jest and react-router-dom : React. createElement : type is invalid -- expected a string (for built-in components) . .. 但得到:undefined