最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jest: how to count call from mock methods called via `call` or `apply`? - Stack Overflow

programmeradmin11浏览0评论

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?

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?

Share Improve this question edited Mar 3, 2019 at 20:45 llanfair asked Mar 3, 2019 at 14:04 llanfairllanfair 1,8855 gold badges29 silver badges43 bronze badges 2
  • In your example myLib does not have a arrangeViewing method. Could you please update the example , otherwise it's quite hard to get what you are trying. – Andreas Köberle Commented Mar 3, 2019 at 16:18
  • Sorry, I removed the wrong methods from the example. Updated. – llanfair Commented Mar 3, 2019 at 20:46
Add a comment  | 

1 Answer 1

Reset to default 14

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
});
发布评论

评论列表(0)

  1. 暂无评论