-
Notifications
You must be signed in to change notification settings - Fork 242
/
toHaveBeenCalledBefore.ts
40 lines (35 loc) 路 1.09 KB
/
toHaveBeenCalledBefore.ts
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
// Make this file a module
// See: https://github.com/microsoft/TypeScript/issues/17736
export {};
declare global {
namespace jest {
interface Matchers<R, T> {
toHaveBeenCalledBefore(spy: SpyInstance): R;
}
}
}
function toHaveBeenCalledBefore(
this: jest.MatcherUtils,
firstSpy: jest.SpyInstance,
secondSpy: jest.SpyInstance,
): { message(): string; pass: boolean } {
const firstSpyEarliestCall = Math.min(...firstSpy.mock.invocationCallOrder);
const secondSpyEarliestCall = Math.min(...secondSpy.mock.invocationCallOrder);
const pass = firstSpyEarliestCall < secondSpyEarliestCall;
const message = pass
? () =>
this.utils.matcherHint('.not.toHaveBeenCalledBefore') +
'\n\n' +
`Expected ${firstSpy.getMockName()} not to have been called before ${secondSpy.getMockName()}`
: () =>
this.utils.matcherHint('.toHaveBeenCalledBefore') +
'\n\n' +
`Expected ${firstSpy.getMockName()} to have been called before ${secondSpy.getMockName()}`;
return {
message,
pass,
};
}
expect.extend({
toHaveBeenCalledBefore,
});