llanfair
llanfair

Reputation: 1885

Jest: how to count call from mock methods called via `call` or `apply`?

How can I use mocks to count function calls made via call or apply

// mylib.js
module.exports = {
  requestInfo: function(model, id) {
    return `The information for ${model} with ID ${id} is foobar`;
  },
  execute: function(name) {
    return this[name] && this[name].apply(this, [].slice.call(arguments, 1));
  },
};
// mylib.test.js
jest.mock('./mylib.js');

var myLib = require('./mylib.js');

test('', () => {
  myLib.execute('requestInfo', 'Ferrari', '14523');
  expect(myLib.execute.mock.calls.length).toBe(1); // Success!
  expect(myLib.requestInfo.mock.calls.length).toBe(1); // FAIL
});

If I explicitly call myLib.requestInfo, the second expectation succeeds.

Is there a way to watch module mock calls whose functions were called via apply or call?

Upvotes: 6

Views: 22203

Answers (1)

Brian Adams
Brian Adams

Reputation: 45780

From the jest.mock doc:

Mocks a module with an auto-mocked version when it is being required.

The docs could probably be improved with a better description of what "auto-mocked version" means, but what happens is that Jest keeps the API surface of the module the same while replacing the implementation with empty mock functions.


So in this case execute is getting called but it has been replaced by an empty mock function so requestInfo never gets called which causes the test to fail.


To keep the implementation of execute intact you will want to avoid auto-mocking the entire module and instead spy on the original function with something like jest.spyOn:

var myLib = require('./mylib.js');

test('', () => {
  jest.spyOn(myLib, 'execute');  // spy on execute
  jest.spyOn(myLib, 'requestInfo')  // spy on requestInfo...
    .mockImplementation(() => {});  // ...and optionally replace the implementation
  myLib.execute('requestInfo', 'Ferrari', '14523');
  expect(myLib.execute.mock.calls.length).toBe(1); // SUCCESS
  expect(myLib.requestInfo.mock.calls.length).toBe(1); // SUCCESS
});

Upvotes: 14

Related Questions