How Do I Test Call And Apply Functions In Jest?
Here's my callnapply.js file const callAndApply = { caller(object, method, nameArg, ageArg, tShirtSizeArg) { method.call(object, nameArg, ageArg, tShirtSizeArg); }, appli
Solution 1:
You can mock the .call()
method itself:
const outer = function() {}
outer.call = jest.fn()
Then you can do usual jest mock tests on outer.call
.
Here is the working test file:
const callnapply = require('./callnapply');
test('testing Function.prototype.call as mock function', () => {
const outer = function() {};
outer.call = jest.fn();
const name = 'Aakash';
const age = 22;
const tee = 'M';
callnapply.caller(this, outer, name, age, tee);
expect(outer.call).toHaveBeenCalledWith(this, name, age, tee);
});
See this working REPL.
Post a Comment for "How Do I Test Call And Apply Functions In Jest?"