Cómo burlarse de una const exportada en broma
Tengo un archivo que se basa en un archivo exportado.const
variable. Esta variable se establece entrue
pero si alguna vez se necesita se puede establecer enfalse
manualmente para evitar algún comportamiento si los servicios posteriores lo solicitan.
No estoy seguro de cómo burlarme de unconst
variable en Jest para que pueda cambiar su valor para probar eltrue
yfalse
condiciones
Ejemplo:
//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
});
});