Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

353
Views
How to prevent Jest from seeing a function being called or implement function differently?

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.

jest seeing error from doSomething

about 4 years ago ยท Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!