如何在开玩笑中嘲笑导出的const
2022-08-30 04:58:52
我有一个依赖于导出变量的文件。此变量设置为,但如果需要,可以手动设置为手动,以防止下游服务请求时的某些行为。const
true
false
我不确定如何在Jest中模拟变量,以便我可以更改其值以测试和条件。const
true
false
例:
//constants module
export const ENABLED = true;
//allowThrough module
import { ENABLED } from './constants';
export function allowThrough(data) {
return (data && ENABLED === true)
}
// jest test
import { allowThrough } from './allowThrough';
import { ENABLED } from './constants';
describe('allowThrough', () => {
test('success', () => {
expect(ENABLED).toBE(true);
expect(allowThrough({value: 1})).toBe(true);
});
test('fail, ENABLED === false', () => {
//how do I override the value of ENABLED here?
expect(ENABLED).toBe(false) // won't work because enabled is a const
expect(allowThrough({value: 1})).toBe(true); //fails because ENABLED is still true
});
});