File that exports a constant value that I want to mock:
// utils/deviceTypeUtils file
import DeviceInfo from 'react-native-device-info';
export const isTablet = DeviceInfo.isTablet();
In my test file, I use this code to mock the constant isTablet
:
// file: deviceTypeUtils.spec
const DeviceTypeUtilsMock = 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;
});
});