Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda