Mock promisified function

The code

1
2
3
4
5
6
import MyClass from 'my-module';

const myObject = new MyClass('param-1', 'param-2');
const createPromise = promisify(myObject.create).bind(myObject);

const result = await createPromise('some-param');

The test

1
2
3
4
5
6
7
8
9
10
11
const mockCreate = jest.fn((_params, callback) => {
callback(null, { foo: 'bar' });
});

jest.mock('my-module', () => {
return jest.fn().mockImplementation(() => ({
create: mockCreate,
}));
});

expect(mockCreate).toHaveBeenCalledTimes(1);