Mock Class Constructor in Jest Test with Mocking Partials

I’ve been implementing tests on server-side code using Jest. One thing I initially struggled with was mocking class constructors. Here’s a guide on how to do it 🙂

Example Code

Here is the code for the function you want to test:

export c…


This content originally appeared on DEV Community and was authored by Kota Ito

I've been implementing tests on server-side code using Jest. One thing I initially struggled with was mocking class constructors. Here’s a guide on how to do it :)

Example Code

Here is the code for the function you want to test:

export const abc=(arg)=>{
return new ClassA(arg)
}

In the test, you want to verify that the function abc returns a new instance of ClassA and that the ClassA constructor is called with arg. The first step is to mock the ClassA constructor.

How to mock the Class Constructor?

You can mock the Class Constructor and even its implementation with mock function from Jest .

Here is how you do it.

jest.mock('path for ClassA', () => ({
  ClassA: jest.fn().mockImplementation(() => ({ ClassA: 'dummyClassAResult' })),
}));

In the code above, I am using a partial mock, meaning I’m only mocking part of the module where ClassA is defined.

The module is mocked so that ClassA is a Jest function. Additionally, I completely replace the implementation of the mock function to return an object { ClassA: 'dummyClassAResult' }.

Note that you cannot use mockReturnValue to mock the return value of a class constructor. Instead, you should use mockImplementation to achieve this.

Here is the incorrect approach:

jest.mock('ClassA module path', () => ({
  ClassA: jest.fn().mockReturnValue({ ClassA: 'dummyClassAResult' })
}));

Using mockReturnValue does not work for class constructors because it is intended for mocking the return values of functions. When mocking class constructors, you need to replace the implementation of the constructor itself to ensure the class instance is created correctly.

Official Documentation for Jest mock

How to Keep Some Parts of a Mocked Module

If you mock a module as shown above, only ClassA is mocked. This means that other functions and objects exported from the module will not be mocked and will show as undefined when imported. So, what if you want to keep some of them intact? You can use the jest.requireActual function for such cases.

import { originalExportedObject, ClassA } from 'ClassA module path';

jest.mock('ClassA module path', () => {
  const actualModule = jest.requireActual('ClassA module path');
  return {
    ...actualModule,
    ClassA: jest.fn().mockImplementation(() => ({ ClassA: 'dummyClassAResult' })),
  };
});

In the code above, jest.requireActual is used to extract the original exports from the module. This allows you to keep originalExportedObject and other parts of the module unchanged, while only ClassA is mocked.

Time to write a test

  //import ClassA
  import {ClassA} from 'ClassA module path'

  //mock the module
  jest.mock('ClassA module path', () => ({ 
  ClassA: jest.fn()
  .mockImplementation(() => ({ ClassA:'dummyClassAResult' })),
  }));

  //test
  it('Returning ', () => { 
      const arg='dummyArg'
      const result=abc(arg)

      expect(ClassA).toHaveBeenCalledWith(arg); //// Since ClassA is now a Jest function, you can use assertions on it!
      expect(result).toStrictEqual({ ClassA: 'dummyClassAResult' })
    });

I hope this helps my fellow developers to write test on their code!


This content originally appeared on DEV Community and was authored by Kota Ito


Print Share Comment Cite Upload Translate Updates
APA

Kota Ito | Sciencx (2024-06-20T22:27:43+00:00) Mock Class Constructor in Jest Test with Mocking Partials. Retrieved from https://www.scien.cx/2024/06/20/mock-class-constructor-in-jest-test-with-mocking-partials/

MLA
" » Mock Class Constructor in Jest Test with Mocking Partials." Kota Ito | Sciencx - Thursday June 20, 2024, https://www.scien.cx/2024/06/20/mock-class-constructor-in-jest-test-with-mocking-partials/
HARVARD
Kota Ito | Sciencx Thursday June 20, 2024 » Mock Class Constructor in Jest Test with Mocking Partials., viewed ,<https://www.scien.cx/2024/06/20/mock-class-constructor-in-jest-test-with-mocking-partials/>
VANCOUVER
Kota Ito | Sciencx - » Mock Class Constructor in Jest Test with Mocking Partials. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/20/mock-class-constructor-in-jest-test-with-mocking-partials/
CHICAGO
" » Mock Class Constructor in Jest Test with Mocking Partials." Kota Ito | Sciencx - Accessed . https://www.scien.cx/2024/06/20/mock-class-constructor-in-jest-test-with-mocking-partials/
IEEE
" » Mock Class Constructor in Jest Test with Mocking Partials." Kota Ito | Sciencx [Online]. Available: https://www.scien.cx/2024/06/20/mock-class-constructor-in-jest-test-with-mocking-partials/. [Accessed: ]
rf:citation
» Mock Class Constructor in Jest Test with Mocking Partials | Kota Ito | Sciencx | https://www.scien.cx/2024/06/20/mock-class-constructor-in-jest-test-with-mocking-partials/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.