index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.printReceived =
  6. exports.printExpected =
  7. exports.printDiffOrStringify =
  8. exports.pluralize =
  9. exports.matcherHint =
  10. exports.matcherErrorMessage =
  11. exports.highlightTrailingWhitespace =
  12. exports.getLabelPrinter =
  13. exports.ensureNumbers =
  14. exports.ensureNoExpected =
  15. exports.ensureExpectedIsNumber =
  16. exports.ensureExpectedIsNonNegativeInteger =
  17. exports.ensureActualIsNumber =
  18. exports.diff =
  19. exports.SUGGEST_TO_CONTAIN_EQUAL =
  20. exports.RECEIVED_COLOR =
  21. exports.INVERTED_COLOR =
  22. exports.EXPECTED_COLOR =
  23. exports.DIM_COLOR =
  24. exports.BOLD_WEIGHT =
  25. void 0;
  26. exports.printWithType = printWithType;
  27. exports.replaceMatchedToAsymmetricMatcher = replaceMatchedToAsymmetricMatcher;
  28. exports.stringify = void 0;
  29. var _chalk = _interopRequireDefault(require('chalk'));
  30. var _jestDiff = require('jest-diff');
  31. var _jestGetType = require('jest-get-type');
  32. var _prettyFormat = require('pretty-format');
  33. var _Replaceable = _interopRequireDefault(require('./Replaceable'));
  34. var _deepCyclicCopyReplaceable = _interopRequireDefault(
  35. require('./deepCyclicCopyReplaceable')
  36. );
  37. function _interopRequireDefault(obj) {
  38. return obj && obj.__esModule ? obj : {default: obj};
  39. }
  40. /**
  41. * Copyright (c) Meta Platforms, Inc. and affiliates.
  42. *
  43. * This source code is licensed under the MIT license found in the
  44. * LICENSE file in the root directory of this source tree.
  45. */
  46. /* eslint-disable local/ban-types-eventually */
  47. const {
  48. AsymmetricMatcher,
  49. DOMCollection,
  50. DOMElement,
  51. Immutable,
  52. ReactElement,
  53. ReactTestComponent
  54. } = _prettyFormat.plugins;
  55. const PLUGINS = [
  56. ReactTestComponent,
  57. ReactElement,
  58. DOMElement,
  59. DOMCollection,
  60. Immutable,
  61. AsymmetricMatcher
  62. ];
  63. // subset of Chalk type
  64. const EXPECTED_COLOR = _chalk.default.green;
  65. exports.EXPECTED_COLOR = EXPECTED_COLOR;
  66. const RECEIVED_COLOR = _chalk.default.red;
  67. exports.RECEIVED_COLOR = RECEIVED_COLOR;
  68. const INVERTED_COLOR = _chalk.default.inverse;
  69. exports.INVERTED_COLOR = INVERTED_COLOR;
  70. const BOLD_WEIGHT = _chalk.default.bold;
  71. exports.BOLD_WEIGHT = BOLD_WEIGHT;
  72. const DIM_COLOR = _chalk.default.dim;
  73. exports.DIM_COLOR = DIM_COLOR;
  74. const MULTILINE_REGEXP = /\n/;
  75. const SPACE_SYMBOL = '\u{00B7}'; // middle dot
  76. const NUMBERS = [
  77. 'zero',
  78. 'one',
  79. 'two',
  80. 'three',
  81. 'four',
  82. 'five',
  83. 'six',
  84. 'seven',
  85. 'eight',
  86. 'nine',
  87. 'ten',
  88. 'eleven',
  89. 'twelve',
  90. 'thirteen'
  91. ];
  92. const SUGGEST_TO_CONTAIN_EQUAL = _chalk.default.dim(
  93. 'Looks like you wanted to test for object/array equality with the stricter `toContain` matcher. You probably need to use `toContainEqual` instead.'
  94. );
  95. exports.SUGGEST_TO_CONTAIN_EQUAL = SUGGEST_TO_CONTAIN_EQUAL;
  96. const stringify = (object, maxDepth = 10, maxWidth = 10) => {
  97. const MAX_LENGTH = 10000;
  98. let result;
  99. try {
  100. result = (0, _prettyFormat.format)(object, {
  101. maxDepth,
  102. maxWidth,
  103. min: true,
  104. plugins: PLUGINS
  105. });
  106. } catch {
  107. result = (0, _prettyFormat.format)(object, {
  108. callToJSON: false,
  109. maxDepth,
  110. maxWidth,
  111. min: true,
  112. plugins: PLUGINS
  113. });
  114. }
  115. if (result.length >= MAX_LENGTH && maxDepth > 1) {
  116. return stringify(object, Math.floor(maxDepth / 2), maxWidth);
  117. } else if (result.length >= MAX_LENGTH && maxWidth > 1) {
  118. return stringify(object, maxDepth, Math.floor(maxWidth / 2));
  119. } else {
  120. return result;
  121. }
  122. };
  123. exports.stringify = stringify;
  124. const highlightTrailingWhitespace = text =>
  125. text.replace(/\s+$/gm, _chalk.default.inverse('$&'));
  126. // Instead of inverse highlight which now implies a change,
  127. // replace common spaces with middle dot at the end of any line.
  128. exports.highlightTrailingWhitespace = highlightTrailingWhitespace;
  129. const replaceTrailingSpaces = text =>
  130. text.replace(/\s+$/gm, spaces => SPACE_SYMBOL.repeat(spaces.length));
  131. const printReceived = object =>
  132. RECEIVED_COLOR(replaceTrailingSpaces(stringify(object)));
  133. exports.printReceived = printReceived;
  134. const printExpected = value =>
  135. EXPECTED_COLOR(replaceTrailingSpaces(stringify(value)));
  136. exports.printExpected = printExpected;
  137. function printWithType(name, value, print) {
  138. const type = (0, _jestGetType.getType)(value);
  139. const hasType =
  140. type !== 'null' && type !== 'undefined'
  141. ? `${name} has type: ${type}\n`
  142. : '';
  143. const hasValue = `${name} has value: ${print(value)}`;
  144. return hasType + hasValue;
  145. }
  146. const ensureNoExpected = (expected, matcherName, options) => {
  147. if (typeof expected !== 'undefined') {
  148. // Prepend maybe not only for backward compatibility.
  149. const matcherString = (options ? '' : '[.not]') + matcherName;
  150. throw new Error(
  151. matcherErrorMessage(
  152. matcherHint(matcherString, undefined, '', options),
  153. // Because expected is omitted in hint above,
  154. // expected is black instead of green in message below.
  155. 'this matcher must not have an expected argument',
  156. printWithType('Expected', expected, printExpected)
  157. )
  158. );
  159. }
  160. };
  161. /**
  162. * Ensures that `actual` is of type `number | bigint`
  163. */
  164. exports.ensureNoExpected = ensureNoExpected;
  165. const ensureActualIsNumber = (actual, matcherName, options) => {
  166. if (typeof actual !== 'number' && typeof actual !== 'bigint') {
  167. // Prepend maybe not only for backward compatibility.
  168. const matcherString = (options ? '' : '[.not]') + matcherName;
  169. throw new Error(
  170. matcherErrorMessage(
  171. matcherHint(matcherString, undefined, undefined, options),
  172. `${RECEIVED_COLOR('received')} value must be a number or bigint`,
  173. printWithType('Received', actual, printReceived)
  174. )
  175. );
  176. }
  177. };
  178. /**
  179. * Ensures that `expected` is of type `number | bigint`
  180. */
  181. exports.ensureActualIsNumber = ensureActualIsNumber;
  182. const ensureExpectedIsNumber = (expected, matcherName, options) => {
  183. if (typeof expected !== 'number' && typeof expected !== 'bigint') {
  184. // Prepend maybe not only for backward compatibility.
  185. const matcherString = (options ? '' : '[.not]') + matcherName;
  186. throw new Error(
  187. matcherErrorMessage(
  188. matcherHint(matcherString, undefined, undefined, options),
  189. `${EXPECTED_COLOR('expected')} value must be a number or bigint`,
  190. printWithType('Expected', expected, printExpected)
  191. )
  192. );
  193. }
  194. };
  195. /**
  196. * Ensures that `actual` & `expected` are of type `number | bigint`
  197. */
  198. exports.ensureExpectedIsNumber = ensureExpectedIsNumber;
  199. const ensureNumbers = (actual, expected, matcherName, options) => {
  200. ensureActualIsNumber(actual, matcherName, options);
  201. ensureExpectedIsNumber(expected, matcherName, options);
  202. };
  203. exports.ensureNumbers = ensureNumbers;
  204. const ensureExpectedIsNonNegativeInteger = (expected, matcherName, options) => {
  205. if (
  206. typeof expected !== 'number' ||
  207. !Number.isSafeInteger(expected) ||
  208. expected < 0
  209. ) {
  210. // Prepend maybe not only for backward compatibility.
  211. const matcherString = (options ? '' : '[.not]') + matcherName;
  212. throw new Error(
  213. matcherErrorMessage(
  214. matcherHint(matcherString, undefined, undefined, options),
  215. `${EXPECTED_COLOR('expected')} value must be a non-negative integer`,
  216. printWithType('Expected', expected, printExpected)
  217. )
  218. );
  219. }
  220. };
  221. // Given array of diffs, return concatenated string:
  222. // * include common substrings
  223. // * exclude change substrings which have opposite op
  224. // * include change substrings which have argument op
  225. // with inverse highlight only if there is a common substring
  226. exports.ensureExpectedIsNonNegativeInteger = ensureExpectedIsNonNegativeInteger;
  227. const getCommonAndChangedSubstrings = (diffs, op, hasCommonDiff) =>
  228. diffs.reduce(
  229. (reduced, diff) =>
  230. reduced +
  231. (diff[0] === _jestDiff.DIFF_EQUAL
  232. ? diff[1]
  233. : diff[0] !== op
  234. ? ''
  235. : hasCommonDiff
  236. ? INVERTED_COLOR(diff[1])
  237. : diff[1]),
  238. ''
  239. );
  240. const isLineDiffable = (expected, received) => {
  241. const expectedType = (0, _jestGetType.getType)(expected);
  242. const receivedType = (0, _jestGetType.getType)(received);
  243. if (expectedType !== receivedType) {
  244. return false;
  245. }
  246. if ((0, _jestGetType.isPrimitive)(expected)) {
  247. // Print generic line diff for strings only:
  248. // * if neither string is empty
  249. // * if either string has more than one line
  250. return (
  251. typeof expected === 'string' &&
  252. typeof received === 'string' &&
  253. expected.length !== 0 &&
  254. received.length !== 0 &&
  255. (MULTILINE_REGEXP.test(expected) || MULTILINE_REGEXP.test(received))
  256. );
  257. }
  258. if (
  259. expectedType === 'date' ||
  260. expectedType === 'function' ||
  261. expectedType === 'regexp'
  262. ) {
  263. return false;
  264. }
  265. if (expected instanceof Error && received instanceof Error) {
  266. return false;
  267. }
  268. if (
  269. receivedType === 'object' &&
  270. typeof received.asymmetricMatch === 'function'
  271. ) {
  272. return false;
  273. }
  274. return true;
  275. };
  276. const MAX_DIFF_STRING_LENGTH = 20000;
  277. const printDiffOrStringify = (
  278. expected,
  279. received,
  280. expectedLabel,
  281. receivedLabel,
  282. expand // CLI options: true if `--expand` or false if `--no-expand`
  283. ) => {
  284. if (
  285. typeof expected === 'string' &&
  286. typeof received === 'string' &&
  287. expected.length !== 0 &&
  288. received.length !== 0 &&
  289. expected.length <= MAX_DIFF_STRING_LENGTH &&
  290. received.length <= MAX_DIFF_STRING_LENGTH &&
  291. expected !== received
  292. ) {
  293. if (expected.includes('\n') || received.includes('\n')) {
  294. return (0, _jestDiff.diffStringsUnified)(expected, received, {
  295. aAnnotation: expectedLabel,
  296. bAnnotation: receivedLabel,
  297. changeLineTrailingSpaceColor: _chalk.default.bgYellow,
  298. commonLineTrailingSpaceColor: _chalk.default.bgYellow,
  299. emptyFirstOrLastLinePlaceholder: '↵',
  300. // U+21B5
  301. expand,
  302. includeChangeCounts: true
  303. });
  304. }
  305. const diffs = (0, _jestDiff.diffStringsRaw)(expected, received, true);
  306. const hasCommonDiff = diffs.some(diff => diff[0] === _jestDiff.DIFF_EQUAL);
  307. const printLabel = getLabelPrinter(expectedLabel, receivedLabel);
  308. const expectedLine =
  309. printLabel(expectedLabel) +
  310. printExpected(
  311. getCommonAndChangedSubstrings(
  312. diffs,
  313. _jestDiff.DIFF_DELETE,
  314. hasCommonDiff
  315. )
  316. );
  317. const receivedLine =
  318. printLabel(receivedLabel) +
  319. printReceived(
  320. getCommonAndChangedSubstrings(
  321. diffs,
  322. _jestDiff.DIFF_INSERT,
  323. hasCommonDiff
  324. )
  325. );
  326. return `${expectedLine}\n${receivedLine}`;
  327. }
  328. if (isLineDiffable(expected, received)) {
  329. const {replacedExpected, replacedReceived} =
  330. replaceMatchedToAsymmetricMatcher(expected, received, [], []);
  331. const difference = (0, _jestDiff.diff)(replacedExpected, replacedReceived, {
  332. aAnnotation: expectedLabel,
  333. bAnnotation: receivedLabel,
  334. expand,
  335. includeChangeCounts: true
  336. });
  337. if (
  338. typeof difference === 'string' &&
  339. difference.includes(`- ${expectedLabel}`) &&
  340. difference.includes(`+ ${receivedLabel}`)
  341. ) {
  342. return difference;
  343. }
  344. }
  345. const printLabel = getLabelPrinter(expectedLabel, receivedLabel);
  346. const expectedLine = printLabel(expectedLabel) + printExpected(expected);
  347. const receivedLine =
  348. printLabel(receivedLabel) +
  349. (stringify(expected) === stringify(received)
  350. ? 'serializes to the same string'
  351. : printReceived(received));
  352. return `${expectedLine}\n${receivedLine}`;
  353. };
  354. // Sometimes, e.g. when comparing two numbers, the output from jest-diff
  355. // does not contain more information than the `Expected:` / `Received:` already gives.
  356. // In those cases, we do not print a diff to make the output shorter and not redundant.
  357. exports.printDiffOrStringify = printDiffOrStringify;
  358. const shouldPrintDiff = (actual, expected) => {
  359. if (typeof actual === 'number' && typeof expected === 'number') {
  360. return false;
  361. }
  362. if (typeof actual === 'bigint' && typeof expected === 'bigint') {
  363. return false;
  364. }
  365. if (typeof actual === 'boolean' && typeof expected === 'boolean') {
  366. return false;
  367. }
  368. return true;
  369. };
  370. function replaceMatchedToAsymmetricMatcher(
  371. replacedExpected,
  372. replacedReceived,
  373. expectedCycles,
  374. receivedCycles
  375. ) {
  376. return _replaceMatchedToAsymmetricMatcher(
  377. (0, _deepCyclicCopyReplaceable.default)(replacedExpected),
  378. (0, _deepCyclicCopyReplaceable.default)(replacedReceived),
  379. expectedCycles,
  380. receivedCycles
  381. );
  382. }
  383. function _replaceMatchedToAsymmetricMatcher(
  384. replacedExpected,
  385. replacedReceived,
  386. expectedCycles,
  387. receivedCycles
  388. ) {
  389. if (!_Replaceable.default.isReplaceable(replacedExpected, replacedReceived)) {
  390. return {
  391. replacedExpected,
  392. replacedReceived
  393. };
  394. }
  395. if (
  396. expectedCycles.includes(replacedExpected) ||
  397. receivedCycles.includes(replacedReceived)
  398. ) {
  399. return {
  400. replacedExpected,
  401. replacedReceived
  402. };
  403. }
  404. expectedCycles.push(replacedExpected);
  405. receivedCycles.push(replacedReceived);
  406. const expectedReplaceable = new _Replaceable.default(replacedExpected);
  407. const receivedReplaceable = new _Replaceable.default(replacedReceived);
  408. expectedReplaceable.forEach((expectedValue, key) => {
  409. const receivedValue = receivedReplaceable.get(key);
  410. if (isAsymmetricMatcher(expectedValue)) {
  411. if (expectedValue.asymmetricMatch(receivedValue)) {
  412. receivedReplaceable.set(key, expectedValue);
  413. }
  414. } else if (isAsymmetricMatcher(receivedValue)) {
  415. if (receivedValue.asymmetricMatch(expectedValue)) {
  416. expectedReplaceable.set(key, receivedValue);
  417. }
  418. } else if (
  419. _Replaceable.default.isReplaceable(expectedValue, receivedValue)
  420. ) {
  421. const replaced = _replaceMatchedToAsymmetricMatcher(
  422. expectedValue,
  423. receivedValue,
  424. expectedCycles,
  425. receivedCycles
  426. );
  427. expectedReplaceable.set(key, replaced.replacedExpected);
  428. receivedReplaceable.set(key, replaced.replacedReceived);
  429. }
  430. });
  431. return {
  432. replacedExpected: expectedReplaceable.object,
  433. replacedReceived: receivedReplaceable.object
  434. };
  435. }
  436. function isAsymmetricMatcher(data) {
  437. const type = (0, _jestGetType.getType)(data);
  438. return type === 'object' && typeof data.asymmetricMatch === 'function';
  439. }
  440. const diff = (a, b, options) =>
  441. shouldPrintDiff(a, b) ? (0, _jestDiff.diff)(a, b, options) : null;
  442. exports.diff = diff;
  443. const pluralize = (word, count) =>
  444. `${NUMBERS[count] || count} ${word}${count === 1 ? '' : 's'}`;
  445. // To display lines of labeled values as two columns with monospace alignment:
  446. // given the strings which will describe the values,
  447. // return function which given each string, returns the label:
  448. // string, colon, space, and enough padding spaces to align the value.
  449. exports.pluralize = pluralize;
  450. const getLabelPrinter = (...strings) => {
  451. const maxLength = strings.reduce(
  452. (max, string) => (string.length > max ? string.length : max),
  453. 0
  454. );
  455. return string => `${string}: ${' '.repeat(maxLength - string.length)}`;
  456. };
  457. exports.getLabelPrinter = getLabelPrinter;
  458. const matcherErrorMessage = (
  459. hint,
  460. generic,
  461. specific // incorrect value returned from call to printWithType
  462. ) =>
  463. `${hint}\n\n${_chalk.default.bold('Matcher error')}: ${generic}${
  464. typeof specific === 'string' ? `\n\n${specific}` : ''
  465. }`;
  466. // Display assertion for the report when a test fails.
  467. // New format: rejects/resolves, not, and matcher name have black color
  468. // Old format: matcher name has dim color
  469. exports.matcherErrorMessage = matcherErrorMessage;
  470. const matcherHint = (
  471. matcherName,
  472. received = 'received',
  473. expected = 'expected',
  474. options = {}
  475. ) => {
  476. const {
  477. comment = '',
  478. expectedColor = EXPECTED_COLOR,
  479. isDirectExpectCall = false,
  480. // seems redundant with received === ''
  481. isNot = false,
  482. promise = '',
  483. receivedColor = RECEIVED_COLOR,
  484. secondArgument = '',
  485. secondArgumentColor = EXPECTED_COLOR
  486. } = options;
  487. let hint = '';
  488. let dimString = 'expect'; // concatenate adjacent dim substrings
  489. if (!isDirectExpectCall && received !== '') {
  490. hint += DIM_COLOR(`${dimString}(`) + receivedColor(received);
  491. dimString = ')';
  492. }
  493. if (promise !== '') {
  494. hint += DIM_COLOR(`${dimString}.`) + promise;
  495. dimString = '';
  496. }
  497. if (isNot) {
  498. hint += `${DIM_COLOR(`${dimString}.`)}not`;
  499. dimString = '';
  500. }
  501. if (matcherName.includes('.')) {
  502. // Old format: for backward compatibility,
  503. // especially without promise or isNot options
  504. dimString += matcherName;
  505. } else {
  506. // New format: omit period from matcherName arg
  507. hint += DIM_COLOR(`${dimString}.`) + matcherName;
  508. dimString = '';
  509. }
  510. if (expected === '') {
  511. dimString += '()';
  512. } else {
  513. hint += DIM_COLOR(`${dimString}(`) + expectedColor(expected);
  514. if (secondArgument) {
  515. hint += DIM_COLOR(', ') + secondArgumentColor(secondArgument);
  516. }
  517. dimString = ')';
  518. }
  519. if (comment !== '') {
  520. dimString += ` // ${comment}`;
  521. }
  522. if (dimString !== '') {
  523. hint += DIM_COLOR(dimString);
  524. }
  525. return hint;
  526. };
  527. exports.matcherHint = matcherHint;