Open leosuncin opened 2 years ago
Hello future Googlers - I am sending this PSA from 2023 to your time:
The same workaround linked in this issue (jest-create-mock-instance#23) can be applied to jest-mock-extended:
const webSocket = mock<ws.WebSocket>()
Object.setPrototypeOf(webSocket, ws.WebSocket.prototype)
expect(webSocket instanceof ws.WebSocket).toBe(true)
As a feature request, I would say this should not be the default behavior since it can have unexpected side effects. But if it were enabled by some argument to the mock
function, that would be handy (and perhaps self-documenting). For example:
const webSocket = mock<ws.WebSocket>({}, {
fakeInstanceOf: true # Proposed, not yet implemented
})
Would welcome a PR with the above proposal.
@j1mmie, your answer helps a lot. Thanks!
However, after using Object.setPrototypeOf
all mock's methods become undefined. Yet it will work fine if your class defines all methods as an arrow function property.
Also, using jest-create-mock-instance was not an ideal solution for me. It has a vice-versa issue of disability to mock arrow function properties (jest-create-mock-instance#34).
In the end, I come up with a temporary solution to use jest-mock-extended and jest-create-mock-instance together to compensate limitation:
import { createMockInstance } from 'jest-create-mock-instance';
import { mock } from 'jest-mock-extended';
type Constructable<T> = { new(...args: any[]): T } | { (...args: any[]): T };
type Class<T> = Constructable<T> & { prototype: T };
export function customMock<T>(instanceofClass: Class<T>) {
const mockInstant = mock<T>();
Object.setPrototypeOf(mockInstant, instanceofClass.prototype);
const mockWithMethods = createMockInstance(instanceofClass);
Object.assign(mockInstant, mockWithMethods);
return mockInstant;
}
Also including tests, I required from customMock
to pass:
class Dog {
barks() { return 'bark'; }
barksTwice = () => 'bark bark';
}
describe('customMock', () => {
it('could pass instanceof check for mocked class type', () => {
const dogMock = customMock(Dog);
expect(dogMock instanceof Dog).toBeTruthy();
expect(dogMock instanceof Error).toBeFalsy();
});
it('could substitute implementation of a function', () => {
const dogMock = customMock(Dog);
dogMock.barks.mockReturnValue('mock bark');
expect(dogMock.barks()).toStrictEqual('mock bark');
});
it('could substitute implementation of an arrow function', () => {
const dogMock = customMock(Dog);
dogMock.barksTwice.mockReturnValue('mock bark bark');
expect(dogMock.barksTwice()).toStrictEqual('mock bark bark');
});
});
The following test does'nt work because of
instanceof
operatorI tried to create an instance of
Repository
and then override the properties withmock
, it doesn't workThe only way I found to "solved it" is defining the mocks manually
PS: there's another way but using jest-create-mock-instance#23