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

60
Visualizações
jest mockResolvedValueOnce n times

I have code that (greatly simplified and with pseudocode) looks like this:

obj1 = [1,2,3],
obj2 = "foo"

returned_values = []

for item in obj1:
    returned_values.push(connection.query(item))
returned_values.push(connection.query(obj2)

The calls of item and obj2 should return different values from the mocked db.

We could mock it like this:

jest.spyOn(connection, "query")
    .mockResolvedValueOnce("foo")
    .mockResolvedValueOnce("foo")
    .mockResolvedValueOnce("foo")
    .mockResolvedValueOnce("bar")

and call it a day.

Problem is: I have different test cases with obj1 having different lengths - it can be an empty array, it can have 1 element, or 10. I know how many it has before runtime.

Is there something like "mockResolvedValueNTimes(times, value)" or an approach that lets me mock the same return value n times?

I cant flip the order in the real code in which the DB calls are made. The last call depends on the calls b4

about 4 years ago · Juan Pablo Isaza
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