jest.mock(module)和jest.fn()之间的区别是什么?

时间:2016-12-11 17:54:24

标签: reactjs jestjs testunit enzyme

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

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.

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

P.S。代码可以在这里找到:Write a Unit test in Jest for a React form

1 个答案:

答案 0 :(得分:7)

在第一个示例中,您说当在其他模块中导入模块'../src/data/server'时,它将是{server: {report: jest.fn()}}。在下一行中,您尝试使用server。问题是你永远不会导入你模拟的模块。也许在您测试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替换一个模块,只使用path参数调用它,或者使用函数的返回值将其作为第二个参数替换。因此,在您的第一个示例中,当您想要在其中一个文件中进行测试或测试时,'../src/data/server'将被导入{server: {report: jest.fn()}}

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

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

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函数。然后,您可以期望使用正确的参数调用它。

顺便说一下。你在测试中尝试检查功能,在调用间谍时你传递的函数不会起作用,你传递给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"});