1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.printReceivedStringContainExpectedSubstring =
|
7 | exports.printReceivedStringContainExpectedResult =
|
8 | exports.printReceivedConstructorNameNot =
|
9 | exports.printReceivedConstructorName =
|
10 | exports.printReceivedArrayContainExpectedItem =
|
11 | exports.printExpectedConstructorNameNot =
|
12 | exports.printExpectedConstructorName =
|
13 | exports.printCloseTo =
|
14 | void 0;
|
15 |
|
16 | var _jestMatcherUtils = require('jest-matcher-utils');
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | const printSubstring = val => val.replace(/"|\\/g, '\\$&');
|
30 |
|
31 | const printReceivedStringContainExpectedSubstring = (
|
32 | received,
|
33 | start,
|
34 | length
|
35 | ) =>
|
36 | (0, _jestMatcherUtils.RECEIVED_COLOR)(
|
37 | '"' +
|
38 | printSubstring(received.slice(0, start)) +
|
39 | (0, _jestMatcherUtils.INVERTED_COLOR)(
|
40 | printSubstring(received.slice(start, start + length))
|
41 | ) +
|
42 | printSubstring(received.slice(start + length)) +
|
43 | '"'
|
44 | );
|
45 |
|
46 | exports.printReceivedStringContainExpectedSubstring =
|
47 | printReceivedStringContainExpectedSubstring;
|
48 |
|
49 | const printReceivedStringContainExpectedResult = (received, result) =>
|
50 | result === null
|
51 | ? (0, _jestMatcherUtils.printReceived)(received)
|
52 | : printReceivedStringContainExpectedSubstring(
|
53 | received,
|
54 | result.index,
|
55 | result[0].length
|
56 | );
|
57 |
|
58 |
|
59 |
|
60 | exports.printReceivedStringContainExpectedResult =
|
61 | printReceivedStringContainExpectedResult;
|
62 |
|
63 | const printReceivedArrayContainExpectedItem = (received, index) =>
|
64 | (0, _jestMatcherUtils.RECEIVED_COLOR)(
|
65 | '[' +
|
66 | received
|
67 | .map((item, i) => {
|
68 | const stringified = (0, _jestMatcherUtils.stringify)(item);
|
69 | return i === index
|
70 | ? (0, _jestMatcherUtils.INVERTED_COLOR)(stringified)
|
71 | : stringified;
|
72 | })
|
73 | .join(', ') +
|
74 | ']'
|
75 | );
|
76 |
|
77 | exports.printReceivedArrayContainExpectedItem =
|
78 | printReceivedArrayContainExpectedItem;
|
79 |
|
80 | const printCloseTo = (receivedDiff, expectedDiff, precision, isNot) => {
|
81 | const receivedDiffString = (0, _jestMatcherUtils.stringify)(receivedDiff);
|
82 | const expectedDiffString = receivedDiffString.includes('e')
|
83 | ? expectedDiff.toExponential(0)
|
84 | : 0 <= precision && precision < 20
|
85 | ?
|
86 |
|
87 | expectedDiff.toFixed(precision + 1)
|
88 | : (0, _jestMatcherUtils.stringify)(expectedDiff);
|
89 | return (
|
90 | `Expected precision: ${isNot ? ' ' : ''} ${(0,
|
91 | _jestMatcherUtils.stringify)(precision)}\n` +
|
92 | `Expected difference: ${isNot ? 'not ' : ''}< ${(0,
|
93 | _jestMatcherUtils.EXPECTED_COLOR)(expectedDiffString)}\n` +
|
94 | `Received difference: ${isNot ? ' ' : ''} ${(0,
|
95 | _jestMatcherUtils.RECEIVED_COLOR)(receivedDiffString)}`
|
96 | );
|
97 | };
|
98 |
|
99 | exports.printCloseTo = printCloseTo;
|
100 |
|
101 | const printExpectedConstructorName = (label, expected) =>
|
102 | printConstructorName(label, expected, false, true) + '\n';
|
103 |
|
104 | exports.printExpectedConstructorName = printExpectedConstructorName;
|
105 |
|
106 | const printExpectedConstructorNameNot = (label, expected) =>
|
107 | printConstructorName(label, expected, true, true) + '\n';
|
108 |
|
109 | exports.printExpectedConstructorNameNot = printExpectedConstructorNameNot;
|
110 |
|
111 | const printReceivedConstructorName = (label, received) =>
|
112 | printConstructorName(label, received, false, false) + '\n';
|
113 |
|
114 | exports.printReceivedConstructorName = printReceivedConstructorName;
|
115 |
|
116 | const printReceivedConstructorNameNot = (label, received, expected) =>
|
117 | typeof expected.name === 'string' &&
|
118 | expected.name.length !== 0 &&
|
119 | typeof received.name === 'string' &&
|
120 | received.name.length !== 0
|
121 | ? printConstructorName(label, received, true, false) +
|
122 | ` ${
|
123 | Object.getPrototypeOf(received) === expected
|
124 | ? 'extends'
|
125 | : 'extends … extends'
|
126 | } ${(0, _jestMatcherUtils.EXPECTED_COLOR)(expected.name)}` +
|
127 | '\n'
|
128 | : printConstructorName(label, received, false, false) + '\n';
|
129 |
|
130 | exports.printReceivedConstructorNameNot = printReceivedConstructorNameNot;
|
131 |
|
132 | const printConstructorName = (label, constructor, isNot, isExpected) =>
|
133 | typeof constructor.name !== 'string'
|
134 | ? `${label} name is not a string`
|
135 | : constructor.name.length === 0
|
136 | ? `${label} name is an empty string`
|
137 | : `${label}: ${!isNot ? '' : isExpected ? 'not ' : ' '}${
|
138 | isExpected
|
139 | ? (0, _jestMatcherUtils.EXPECTED_COLOR)(constructor.name)
|
140 | : (0, _jestMatcherUtils.RECEIVED_COLOR)(constructor.name)
|
141 | }`;
|