Here is the solution:
index.ts, the file will be tested:
import * as dotenv from 'dotenv';
export class TestSubject {
public test() {
console.log(dotenv);
}
}
Unit test:
describe('TestSubject', () => {
beforeEach(() => {
jest.resetModules();
});
it('should mock dotenv', () => {
jest.doMock('dotenv');
const dotenv = require('dotenv');
const { TestSubject } = require('./');
const subject = new TestSubject();
subject.test();
expect(jest.isMockFunction(dotenv.config)).toBeTruthy();
});
it('should not mock dotenv', () => {
jest.unmock('dotenv');
const dotenv = require('dotenv');
const { TestSubject } = require('./');
const subject = new TestSubject();
subject.test();
expect(jest.isMockFunction(dotenv.config)).toBeFalsy();
});
});
Unit test result:
PASS src/stackoverflow/56446543/index.spec.ts
TestSubject
✓ should mock dotenv (2111ms)
✓ should not mock dotenv (2ms)
console.log src/stackoverflow/56446543/index.ts:5
{ config:
{ [Function: config]
_isMockFunction: true,
getMockImplementation: [Function],
mock: [Getter/Setter],
mockClear: [Function],
mockReset: [Function],
mockRestore: [Function],
mockReturnValueOnce: [Function],
mockResolvedValueOnce: [Function],
mockRejectedValueOnce: [Function],
mockReturnValue: [Function],
mockResolvedValue: [Function],
mockRejectedValue: [Function],
mockImplementationOnce: [Function],
mockImplementation: [Function],
mockReturnThis: [Function],
mockName: [Function],
getMockName: [Function] },
parse:
{ [Function: parse]
_isMockFunction: true,
getMockImplementation: [Function],
mock: [Getter/Setter],
mockClear: [Function],
mockReset: [Function],
mockRestore: [Function],
mockReturnValueOnce: [Function],
mockResolvedValueOnce: [Function],
mockRejectedValueOnce: [Function],
mockReturnValue: [Function],
mockResolvedValue: [Function],
mockRejectedValue: [Function],
mockImplementationOnce: [Function],
mockImplementation: [Function],
mockReturnThis: [Function],
mockName: [Function],
getMockName: [Function] },
default:
{ config:
{ [Function: config]
_isMockFunction: true,
getMockImplementation: [Function],
mock: [Getter/Setter],
mockClear: [Function],
mockReset: [Function],
mockRestore: [Function],
mockReturnValueOnce: [Function],
mockResolvedValueOnce: [Function],
mockRejectedValueOnce: [Function],
mockReturnValue: [Function],
mockResolvedValue: [Function],
mockRejectedValue: [Function],
mockImplementationOnce: [Function],
mockImplementation: [Function],
mockReturnThis: [Function],
mockName: [Function],
getMockName: [Function] },
parse:
{ [Function: parse]
_isMockFunction: true,
getMockImplementation: [Function],
mock: [Getter/Setter],
mockClear: [Function],
mockReset: [Function],
mockRestore: [Function],
mockReturnValueOnce: [Function],
mockResolvedValueOnce: [Function],
mockRejectedValueOnce: [Function],
mockReturnValue: [Function],
mockResolvedValue: [Function],
mockRejectedValue: [Function],
mockImplementationOnce: [Function],
mockImplementation: [Function],
mockReturnThis: [Function],
mockName: [Function],
getMockName: [Function] } } }
console.log src/stackoverflow/56446543/index.ts:5
{ config: [Function: config],
parse: [Function: parse],
default: { config: [Function: config], parse: [Function: parse] } }
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 3.394s, estimated 4s
As you can see, the dotenv module is mocked for the first test case and is not mocked for the second test case.
You can find the completed demo here: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/56446543