Tengo un script con una función que se llama al final. Quiero evitar que doSomething() arroje un error en las pruebas de broma.
calculadora.js
const add = (a, b) => { return a + b; } function doSomething(){ throw Error('dang I messed you up') } doSomething(); // 👈 this gets call in Jest test everytime module.exports = { add, somethingElse } En el archivo de prueba, quiero evitar que se llame a doSomething() :
const Calc = require('../calculator.js'); const { add, doSomething } = Calc; // prevent doSomething() from being called 👈 doSomething.mockImplementation(() => console.log('so much nope')); // or jest.spyOn(Calc, 'doSomething').mockImplementation(() => console.log('so much nope')); test('Adding two numbers', async () => { expect(add(5, 5)).toStrictEqual(10) })Jest no está cambiando la implementación, sino que aún muestra el error.