I have a script with a function being called at the end. I want to prevent doSomething() from throwing an error in the jest tests.
calculator.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
}
In test file, I want to prevent doSomething() from being called:
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 isn't changing the implementation, but instead still shows the error.