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

163
Views
How to mock a nested function with Jest

I've tried a couple of approaches but I'm not sure if it's possible to mock a nested function in Jest. I read the official docs where they use axios, but I'm trying to mock a function that is imported, not a module.

for example, I have some function A, and within A I call B. I want to mock B so that it returns true once and then false once.

B is a function I imported that I wrote in another file, and I'm testing A.

so far I have tried to import function B into my test file and create a mock of it, but I don't think it's working how I thought it would

for example I have tried this

import { b } from "../util"
jest.mock("b", () => jest.fn().mockReturnValue(true));

and then my tested file would be

function a() => {
  while b() {
    do something;
  }
}

should I rewrite my test or function I'm testing?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Not sure what kind of results you are getting, but for mocking b you could do:

jest.mock("../util", () => ({
  b:jest.fn().mockReturnValueOnce(true).mockReturnValueOnce(false),
}));
about 4 years ago · Juan Pablo Isaza Report
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!