I'm struggling to mock a constant with Jest on a per test basis. I have it working with the code below, but the mock is "static" - I can't mock it differently for each test.
Code:
// allowList.ts
export const ALLOW_LIST = {
'1234': true
};
// listUtil.ts
import { ALLOW_LIST } from './allowList.ts';
export const checkList = (id: string) => {
if (ALLOW_LIST[id]) return true;
return false;
};
Test (working):
// listUtil.test.ts
import { checkList } from './listUtil';
jest.mock('./listUtil', () => {
return {
'5678': true
};
});
test('in list', () => {
expect(checkList('5678')).toBe(true);
});
test('not in list', () => {
expect(checkList('1234')).toBe(false);
});
What I would like (not working):
// listUtil.test.ts
import { checkList } from './listUtil';
test('in list', () => {
jest.mock('./listUtil', () => {
return {
'5678': true
};
});
expect(checkList('5678')).toBe(true);
});
test('not in list', () => {
jest.mock('./listUtil', () => {
return {
'9123': true
};
});
expect(checkList('1234')).toBe(false);
});
Is what I'm trying to do possible? This post is very similar and appears to work when mocking functions, but I'm having the same issue as the commenters of the accepted answer. I think I'm just not understanding how Jest performs mocking under the hood. I believe the working version works because the mock is hoisted and basically overwrites the real implementation, but I'm not sure how or if I can achieve that in each test.
I think one option would be to expose the ALLOW_LIST via a function:
// allowList.ts
const ALLOW_LIST = {
'1234': true
};
export const getAllowList = () => ALLOW_LIST;
and mock that, but am wondering if that's necessary.