Como zombar de uma const exportada em tom de brincadeira
Eu tenho um arquivo que depende de um exportadoconst
variável. Essa variável está definida comotrue
mas, se necessário, pode ser definido comofalse
manualmente para evitar algum comportamento se os serviços a jusante solicitarem.
Não sei como zombar deconst
variável em Jest para que eu possa mudar o seu valor para testar otrue
efalse
condições.
Exemplo:
//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
});
});