/
toHaveBeenCalledOnceWith.js
47 lines (40 loc) 路 1.55 KB
/
toHaveBeenCalledOnceWith.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { isJestMockOrSpy } from '../utils';
export function toHaveBeenCalledOnceWith(received, expected) {
const { printReceived, printExpected, printWithType, matcherHint } = this.utils;
if (!isJestMockOrSpy(received)) {
return {
pass: false,
message: () =>
matcherHint('.toHaveBeenCalledOnceWith') +
'\n\n' +
`Matcher error: ${printReceived('received')} must be a mock or spy function` +
'\n\n' +
printWithType('Received', received, printReceived),
};
}
const passMessage =
matcherHint('.not.toHaveBeenCalledOnceWith') +
'\n\n' +
`Expected mock function to have been called any amount of times but one with ${printExpected(
expected,
)}, but it was called exactly once with ${printExpected(expected)}.`;
const failOnceMessage =
matcherHint('.toHaveBeenCalledOnceWith') +
'\n\n' +
'Expected mock function to have been called exactly once, but it was called:\n' +
` ${printReceived(received.mock.calls.length)} times`;
const failExpectedMessage =
matcherHint('.toHaveBeenCalledOnceWith') +
'\n\n' +
`Expected mock function to have been called exactly once with ${printReceived(
expected,
)}, but it was called with:\n` +
` ${printReceived(received.mock.calls[0]?.[0])}`;
const passOnce = received.mock.calls.length === 1;
const pass = passOnce && this.equals(expected, received.mock.calls[0][0]);
return {
pass,
message: () => (pass ? passMessage : !passOnce ? failOnceMessage : failExpectedMessage),
actual: received,
};
}