Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda