Search code examples
typescriptunit-testingjestjs

How to test a method which uses `instanceof ` on a mocked dependency


I have a typeguard which checks the instance of a dependency.

  private isObjectOfA(obj: A | B ): obj is A {
    return obj instanceof A;
  }

In the spec file, I have mocked the class A.

jest.mock('./my-package/a', () => {
  return {
    A: jest.fn().mockImplementation(() => {
      return {
        someMethod: jest.fn()
      };
    })
  };
});

import { A } from './my-package/a';

Now during testing, isObjectOfA always returns false ( because in tests the instance of obj is returned as 'Object' instead of 'A'. Maybe due to the mock ??). Is there anyway to overcome this issue? The code for the object creation looks like,

this.myObj = someCondition ? new A() : new B();

Solution

  • In order to pass instanceof check, prototype chain needs to be established, e.g. with Object.create:

    jest.mock('./my-package/a', () => {
      const ActualA = jest.requireActual('./my-package/a');
    
      return {
        A: jest.fn().mockImplementation(() => {
          const a = Object.create(ActualA.prototype);
          return Object.assign(a, { someMethod: jest.fn() });
        })
      };
    });
    

    Class auto-mock will provide correct prototype chain for a mock as well.