This example will work if you compile the syntax of ES6 modules in ES5, because ultimately all module exports belong to the same object that can be changed.
import { allowThrough } from './allowThrough'; import { ENABLED } from './constants'; import * as constants from './constants'; describe('allowThrough', () => { test('success', () => { constants.ENABLED = true; expect(ENABLED).toBe(true); expect(allowThrough({ value: 1 })).toBe(true); }); test('fail, ENABLED === false', () => { constants.ENABLED = false; expect(ENABLED).toBe(false); expect(allowThrough({ value: 1 })).toBe(false); }); });
Alternatively, you can switch to raw commonjs require , and do it with jest.mock(...) :
const mockTrue = { ENABLED: true }; const mockFalse = { ENABLED: false }; describe('allowThrough', () => { beforeEach(() => { jest.resetModules(); }); test('success', () => { jest.mock('./constants', () => mockTrue) const { ENABLED } = require('./constants'); const { allowThrough } = require('./allowThrough'); expect(ENABLED).toBe(true); expect(allowThrough({ value: 1 })).toBe(true); }); test('fail, ENABLED === false', () => { jest.mock('./constants', () => mockFalse) const { ENABLED } = require('./constants'); const { allowThrough } = require('./allowThrough'); expect(ENABLED).toBe(false); expect(allowThrough({ value: 1 })).toBe(false); }); });
source share