Mock Functions

Mock Functions

Mock functions make it easy to test the links between code by erasing the actual implementation of a function, capturing calls to the function (and the parameters passed in those calls), capturing instances of constructor functions when instantiated with new, and allowing test-time configuration of return values.

There are two ways to get your hands on mock functions: Either by require()ing a mocked component (via jest.mock('moduleName')) or by explicitly requesting one from jest.fn() in your test:

const myMock = jest.fn();
myMock('1');
myMock('a', 'b');
console.log(myMock.mock.calls);
// > [ [1], ['a', 'b'] ]

.mock property

All mock functions have this special .mock property, which is where data about how the function has been called is kept. The 登录查看完整内容