1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 | var _jestMatcherUtils = require("jest-matcher-utils");
|
5 | var _supportedMatchers = _interopRequireDefault(require("./supported-matchers"));
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const createErrorMessage = spyInfo => {
|
15 | const {
|
16 | spy,
|
17 | pass,
|
18 | calls,
|
19 | matcherName,
|
20 | methodName,
|
21 | expected
|
22 | } = spyInfo;
|
23 | const hint = pass ? `.not${matcherName}` : matcherName;
|
24 | const message = pass ? `Expected mock function not to be called but it was called with:\n${calls.map(_jestMatcherUtils.printReceived)}` : `Expected mock function to be called${expected ? ` with:\n${(0, _jestMatcherUtils.printExpected)(expected)}\n` : '.'}\nbut it was called with:\n${calls.map(_jestMatcherUtils.printReceived)}`;
|
25 | return () => `${(0, _jestMatcherUtils.matcherHint)(hint, spy.getMockName())}` + '\n\n' + message + '\n\n' + `console.${methodName}() should not be used unless explicitly expected\n` + 'See https://www.npmjs.com/package/@wordpress/jest-console for details.';
|
26 | };
|
27 | const createSpyInfo = (spy, matcherName, methodName, expected) => {
|
28 | const calls = spy.mock.calls;
|
29 | const pass = expected ? JSON.stringify(calls).includes(JSON.stringify(expected)) : calls.length > 0;
|
30 | const message = createErrorMessage({
|
31 | spy,
|
32 | pass,
|
33 | calls,
|
34 | matcherName,
|
35 | methodName,
|
36 | expected
|
37 | });
|
38 | return {
|
39 | pass,
|
40 | message
|
41 | };
|
42 | };
|
43 | const createToHaveBeenCalledMatcher = (matcherName, methodName) => received => {
|
44 | const spy = received[methodName];
|
45 | const spyInfo = createSpyInfo(spy, matcherName, methodName);
|
46 | spy.assertionsNumber += 1;
|
47 | return spyInfo;
|
48 | };
|
49 | const createToHaveBeenCalledWith = (matcherName, methodName) => function (received, ...expected) {
|
50 | const spy = received[methodName];
|
51 | const spyInfo = createSpyInfo(spy, matcherName, methodName, expected);
|
52 | spy.assertionsNumber += 1;
|
53 | return spyInfo;
|
54 | };
|
55 | expect.extend(Object.entries(_supportedMatchers.default).reduce((result, [methodName, matcherName]) => {
|
56 | const matcherNameWith = `${matcherName}With`;
|
57 | return {
|
58 | ...result,
|
59 | [matcherName]: createToHaveBeenCalledMatcher(`.${matcherName}`, methodName),
|
60 | [matcherNameWith]: createToHaveBeenCalledWith(`.${matcherNameWith}`, methodName)
|
61 | };
|
62 | }, {}));
|
63 |
|
\ | No newline at end of file |