Fix instanceof checks with Jest mocks
1 min read
If you’re checking a variable type with instanceof and it fails with mocked objects, the prototype chain is broken. Fix it by constructing the mock properly:
someMock = Object.assign(
Object.create(TheClass.prototype),
{ override: jest.fn() }
)
Alternatively:
Object.setPrototypeOf(someMock, TheClass.prototype)
This ensures someMock instanceof TheClass returns true.