Home > Software design >  ignore setup mock for one file
ignore setup mock for one file

Time:07-29

I have a decorator that I use on a lot of my methods. To not have to mock it each time, I have added a mock on jest.setup.js:

jest.mock('src/something', () => {
  someMethod: jest.fn.mockImplementation(/*some implementation*/)
})

This works fine, but now I want to unit test this one method (the someMethod in this example) and I can't, since it brings up the mock. How can I ignore this mock for only this file/test?

CodePudding user response:

You can use jest.unmock(moduleName) in the test file of a module. So that your module under test will use the real module rather than the mocked version.

Let's see an example:

./src/stackoverflow/73129547/something.ts:

export const someMethod = () => 1;

This is the module we want to mock.

./jest.setup.js:

jest.setTimeout(5 * 1000);

jest.mock('./stackoverflow/73129547/something', () => ({
  someMethod: jest.fn().mockReturnValue(0),
}));

jest.config.js:

module.exports = {
  preset: 'ts-jest/presets/js-with-ts',
  testEnvironment: 'jsdom',
  setupFilesAfterEnv: ['./jest.setup.js'],
};

We set up the mock using jest.mock() in the jest.setup.js file. So that every module under test will use the mocked version of this module.

Suppose our project has two modules: a.ts and b.ts, they use the someMethod exported from the something module.

./src/stackoverflow/73129547/a.ts:

import { someMethod } from './something';
export const a = someMethod;

./src/stackoverflow/73129547/b.ts:

import { someMethod } from './something';
export const b = someMethod;

The test suites of a and b modules:

./src/stackoverflow/73129547/a.test.ts:

import { a } from './a';

describe('73129547 - a', () => {
  test('should pass', () => {
    expect(a()).toBe(0);
  });
});

./src/stackoverflow/73129547/b.test.ts:

import { b } from "./b";

describe('73129547 - b', () => {
  test('should pass', () => {
    expect(b()).toBe(0);
  });
});

Test result:

 PASS  stackoverflow/73129547/b.test.ts
 PASS  stackoverflow/73129547/a.test.ts (9.829 s)

Test Suites: 2 passed, 2 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        10.681 s

As you can see the expected execution result. Both the a and b modules use the mocked someMethod which has a mock return value: 0.

Now, we want to test the someMethod of the something module, we should test the real someMethod rather than the mocked one. Test mock implementations make no sense.

./src/stackoverflow/73129547/something.test.ts:

import { someMethod } from './something';

jest.unmock('./something');

describe('73129547 - something', () => {
  test('should pass', () => {
    expect(someMethod()).toBe(1);
  });
});

Test result:

 PASS  stackoverflow/73129547/a.test.ts
 PASS  stackoverflow/73129547/b.test.ts
 PASS  stackoverflow/73129547/something.test.ts

Test Suites: 3 passed, 3 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        1.955 s, estimated 14 s

As you can see, the return value of real someMethod is 1. jest.unmock(moduleName) will unmock the module and return the real module.

  • Related