Jest - Mock A Constant Property From A Module For A Specific Test
So, I'm attempting to do something which on the surface should be very simple... I have some constants defined in: ` //constants.js module.exports = { MY_CONSTANT: 'TEST' } `
Solution 1:
To mock for one test only:
jest.mock('./constants.js', () => ({
MY_CONSTANT: 'something fake'
}));
https://jestjs.io/docs/en/manual-mocks
To provide a mock for every test:
- create a
__mocks__
directory adiacent to the module you desire to mock - provide implementation
- call
jest.mock('./moduleName')
in your test
Solution 2:
File that exports a constant value that I want to mock:
// utils/deviceTypeUtils fileimportDeviceInfofrom'react-native-device-info';
exportconst isTablet = DeviceInfo.isTablet();
In my test file, I use this code to mock the constant isTablet
:
// file: deviceTypeUtils.specconstDeviceTypeUtilsMock = jest.requireMock('../utils/deviceTypeUtils');
jest.mock('../utils/deviceTypeUtils', () => ({
isTablet: false,
}));
describe('mock const example', () => {
it('mock const `isTablet` to the value `true`', () => {
DeviceTypeUtilsMock.isTablet = true;
});
it('mock const `isTablet` to the value `false`', () => {
DeviceTypeUtilsMock.isTablet = false;
});
});
Solution 3:
Follow the guidance of the doc on jest.doMock()
The config.js
file:
export const env = 'test';
The myComponent.js
file:
import {env} from'./config.js';
exportconstMyComponent = () => {
if (env === 'prod') {
return (<Text>Prod Env</Text>);
} elseif (env === 'test'){
return (<Text>Test Env</Text>);
} else {
return (<Text>Dev Env</Text>);
}
};
The myComponent.test.js
file:
describe('Test MyComponent', () => {
test('Test in prod env', () => {
jest.doMock('./config', () => ({env: 'prod'}));
const {MyComponent} = require('./myComponent.js');
const myComp = mount(<MyComponent />);
expect(myComp.find('Text')).toHaveText('Prod Env');
});
test('Test in test env', () => {
jest.doMock('./config', () => ({env: 'test'}));
const {MyComponent} = require('./myComponent.js');
const myComp = mount(<MyComponent />);
expect(myComp.find('Text')).toHaveText('Test Env');
});
test('Test in dev env', () => {
jest.doMock('./config', () => ({env: 'dev'}));
const {MyComponent} = require('./myComponent.js');
const myComp = mount(<MyComponent />);
expect(myComp.find('Text')).toHaveText('Dev Env');
});
});
Solution 4:
I have been able to do something similar by following the post at https://mikeborozdin.com/post/changing-jest-mocks-between-tests/
This creates a mock and then changes it for each test.
import * as constants from'./constants'
jest.mock('./constants', () => ({
__esModule: true,
MY_CONSTANT: 'SOME OTHER DEFAULT',
}))
it("Should do something when MY_CONSTANT === "TEST, () => {
constant.MY_CONSTANT = 'TEST'expect(constant.MY_CONSTANT).toBe('TEST')
})
it("Should do something else when MY_CONSTANT !== "TEST, () => {
constant.MY_CONSTANT = 'NOT A TEST'expect(constant.MY_CONSTANT).toBe('NOT A TEST')
})
If you are using typescript then the mock needs to be cast to modify the mock values:
const mockConstants = constants as { MY_CONSTANT: string }
mockConstants.MY_CONSTANT = 'TEST'
Solution 5:
I just copy constants and assign them after each test case.
const constants = require('./constants');
const original = {...constants};
afterEach(() => {
Object.assign(constants, original);
});
test('test1', () => {
constants.ABC = 'whatever';
});
test('test2', () => {
// constants.ABC is set back to original
});
Post a Comment for "Jest - Mock A Constant Property From A Module For A Specific Test"