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"
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)
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"]