UNPKG

5.25 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.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
16var _jestMatcherUtils = require('jest-matcher-utils');
17
18/**
19 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
20 *
21 * This source code is licensed under the MIT license found in the
22 * LICENSE file in the root directory of this source tree.
23 *
24 */
25
26/* eslint-disable local/ban-types-eventually */
27// Format substring but do not enclose in double quote marks.
28// The replacement is compatible with pretty-format package.
29const printSubstring = val => val.replace(/"|\\/g, '\\$&');
30
31const printReceivedStringContainExpectedSubstring = (
32 received,
33 start,
34 length // not end
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
46exports.printReceivedStringContainExpectedSubstring =
47 printReceivedStringContainExpectedSubstring;
48
49const printReceivedStringContainExpectedResult = (received, result) =>
50 result === null
51 ? (0, _jestMatcherUtils.printReceived)(received)
52 : printReceivedStringContainExpectedSubstring(
53 received,
54 result.index,
55 result[0].length
56 ); // The serialized array is compatible with pretty-format package min option.
57// However, items have default stringify depth (instead of depth - 1)
58// so expected item looks consistent by itself and enclosed in the array.
59
60exports.printReceivedStringContainExpectedResult =
61 printReceivedStringContainExpectedResult;
62
63const 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
77exports.printReceivedArrayContainExpectedItem =
78 printReceivedArrayContainExpectedItem;
79
80const printCloseTo = (receivedDiff, expectedDiff, precision, isNot) => {
81 const receivedDiffString = (0, _jestMatcherUtils.stringify)(receivedDiff);
82 const expectedDiffString = receivedDiffString.includes('e') // toExponential arg is number of digits after the decimal point.
83 ? expectedDiff.toExponential(0)
84 : 0 <= precision && precision < 20 // toFixed arg is number of digits after the decimal point.
85 ? // It may be a value between 0 and 20 inclusive.
86 // Implementations may optionally support a larger range of values.
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
99exports.printCloseTo = printCloseTo;
100
101const printExpectedConstructorName = (label, expected) =>
102 printConstructorName(label, expected, false, true) + '\n';
103
104exports.printExpectedConstructorName = printExpectedConstructorName;
105
106const printExpectedConstructorNameNot = (label, expected) =>
107 printConstructorName(label, expected, true, true) + '\n';
108
109exports.printExpectedConstructorNameNot = printExpectedConstructorNameNot;
110
111const printReceivedConstructorName = (label, received) =>
112 printConstructorName(label, received, false, false) + '\n'; // Do not call function if received is equal to expected.
113
114exports.printReceivedConstructorName = printReceivedConstructorName;
115
116const 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
130exports.printReceivedConstructorNameNot = printReceivedConstructorNameNot;
131
132const 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 }`;