Mock Class Constructor in Jest Test with Mocking Partials
Tags: jest, testing, mock, react
Published: 2024-06-20 22:27:43

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:

```javascript
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.
```javascript
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:
```javascript
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](https://jestjs.io/docs/mock-functions
)


## 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.

```javascript
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

```javascript
  //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!