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

276
Visualizações
Jest truncates list of received mock calls

I have a test that calls a spy multiple times:

// example.test.js
it("Fails but doesn't log all calls", () => {
  const spy = jest.fn();
  spy("1");
  spy("2");
  spy("3");
  spy("4");
  spy("5");
  expect(spy).toHaveBeenCalledWith("not 1");
});

Jest truncates the output, so only the first 3 calls are listed under Received:

// jest example.test.js
expect(jest.fn()).toHaveBeenCalledWith(...expected)

Expected: "not 1"
Received
        1: "1"
        2: "2"
        3: "3"

Number of calls: 5

  10 |   spy("4");
  11 |   spy("5");
> 12 |   expect(spy).toHaveBeenCalledWith("not 1");
      |               ^
  13 | });
  14 |

How can I get the full list of calls to show in the terminal for assertion failures?

Received
        1: "1"
        2: "2"
        3: "3"
        4: "4"
        5: "5"
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can just log to your console the mock calls"

const spy = jest.fn();
  spy("1");
  spy("2");
  spy("3");
  spy("4");
  spy("5");

  console.log(spy.mock.calls)

DOCS -> https://jestjs.io/docs/mock-functions

about 4 years ago · Juan Pablo Isaza Relatório

0

As of June 2022, it is not possible using built-in jest assertions:

const PRINT_LIMIT = 3

Workaround: Write a custom assertion error. See logCalls:

import { printReceived } from "jest-matcher-utils";

const logCalls = (spy) => {
  const receivedCalls = spy.mock.calls.map((call, i) => 
    `${i}: ${printReceived(call)}`
  )
  console.error(`All Received:\n${receivedCalls.join("\n")}`)
}

it("Fails and logs all calls", () => {
  const spy = jest.fn();
  spy("1");
  spy("2");
  spy("3");
  spy("4");
  spy("5");

  try {
    expect(spy).toHaveBeenCalledWith("not 1");
  } catch (e) {
    logCalls(spy)
    throw e
  }
});

Which will output the following error:

All Received:
    0: ["1"]
    1: ["2"]
    2: ["3"]
    3: ["4"]
    4: ["5"]
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