-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Doesn't support instanceof #90
Comments
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:
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
|
Would welcome a PR with the above proposal. |
@j1mmie, your answer helps a lot. Thanks! However, after using 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 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
The text was updated successfully, but these errors were encountered: