How to mock callback functions with jest
Make sure when you call the actual function by passing the callback function as one of the arguments, that function is being called from inside the test block like below
function forEach(items, callback) {
for (let index = 0; index < items.length; index++) {
callback(items[index]);
}
}
const mockCallback = jest.fn((x) => x + 1);
test("fn", () => {
forEach([0, 1, 2], mockCallback);
expect(mockCallback.mock.calls.length).toBe(3);
});
And not like below
function forEach(items, callback) {
for (let index = 0; index < items.length; index++) {
callback(items[index]);
}
}
const mockCallback = jest.fn((x) => x + 1);
forEach([0, 1, 2], mockCallback);
test("fn", () => {
expect(mockCallback.mock.calls.length).toBe(3);
});
You can mock a module with a function that accepts the same parameter as your original one, and instantly call the callback:
jest.mock('../../utilities/imageUtils', () => ({
resizeImage: (file, fileName, callback) => callback('someData')
}));
Btw. the way you mock the module in your question can't work because of the way jest.mock
works. Even if you write it after the let
statement, it will be hoisted to the top of the file when the test is been compiled. So the best way to mock the function with a spy would look like this:
import {resizeImage} from '../../utilities/imageUtils'
jest.mock('../../utilities/imageUtils', () => ({
resizeImage: jest.fn((file, fileName, callback) => callback('someData'))
}));
Now you have the same behaviour as above but you can also test that resizeImage
was called with the correct parameters.
As your last parameter is a function you can either just test for the 2 first params like this using mock.calls
:
expect(resizeImage.mock.calls[0][0]).toBe('firstParameter')
expect(resizeImage.mock.calls[0][1]).toBe('secondParameter')
Or use a wildcard for the last parameter when using toBeCalledWith
using expect.anything()
:
expect(resizeImage).toBeCalledWith('firstParameter', 'secondParameter', expect.anything());