print.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.printReceivedConstructorNameNot = exports.printReceivedConstructorName = exports.printExpectedConstructorNameNot = exports.printExpectedConstructorName = exports.printCloseTo = exports.printReceivedArrayContainExpectedItem = exports.printReceivedStringContainExpectedResult = exports.printReceivedStringContainExpectedSubstring = void 0;
  6. var _jestMatcherUtils = require('jest-matcher-utils');
  7. /**
  8. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  9. *
  10. * This source code is licensed under the MIT license found in the
  11. * LICENSE file in the root directory of this source tree.
  12. *
  13. */
  14. /* eslint-disable local/ban-types-eventually */
  15. // Format substring but do not enclose in double quote marks.
  16. // The replacement is compatible with pretty-format package.
  17. const printSubstring = val => val.replace(/"|\\/g, '\\$&');
  18. const printReceivedStringContainExpectedSubstring = (received, start, length) =>
  19. (0, _jestMatcherUtils.RECEIVED_COLOR)(
  20. '"' +
  21. printSubstring(received.slice(0, start)) +
  22. (0, _jestMatcherUtils.INVERTED_COLOR)(
  23. printSubstring(received.slice(start, start + length))
  24. ) +
  25. printSubstring(received.slice(start + length)) +
  26. '"'
  27. );
  28. exports.printReceivedStringContainExpectedSubstring = printReceivedStringContainExpectedSubstring;
  29. const printReceivedStringContainExpectedResult = (received, result) =>
  30. result === null
  31. ? (0, _jestMatcherUtils.printReceived)(received)
  32. : printReceivedStringContainExpectedSubstring(
  33. received,
  34. result.index,
  35. result[0].length
  36. ); // The serialized array is compatible with pretty-format package min option.
  37. // However, items have default stringify depth (instead of depth - 1)
  38. // so expected item looks consistent by itself and enclosed in the array.
  39. exports.printReceivedStringContainExpectedResult = printReceivedStringContainExpectedResult;
  40. const printReceivedArrayContainExpectedItem = (received, index) =>
  41. (0, _jestMatcherUtils.RECEIVED_COLOR)(
  42. '[' +
  43. received
  44. .map((item, i) => {
  45. const stringified = (0, _jestMatcherUtils.stringify)(item);
  46. return i === index
  47. ? (0, _jestMatcherUtils.INVERTED_COLOR)(stringified)
  48. : stringified;
  49. })
  50. .join(', ') +
  51. ']'
  52. );
  53. exports.printReceivedArrayContainExpectedItem = printReceivedArrayContainExpectedItem;
  54. const printCloseTo = (receivedDiff, expectedDiff, precision, isNot) => {
  55. const receivedDiffString = (0, _jestMatcherUtils.stringify)(receivedDiff);
  56. const expectedDiffString = receivedDiffString.includes('e') // toExponential arg is number of digits after the decimal point.
  57. ? expectedDiff.toExponential(0)
  58. : 0 <= precision && precision < 20 // toFixed arg is number of digits after the decimal point.
  59. ? // It may be a value between 0 and 20 inclusive.
  60. // Implementations may optionally support a larger range of values.
  61. expectedDiff.toFixed(precision + 1)
  62. : (0, _jestMatcherUtils.stringify)(expectedDiff);
  63. return (
  64. `Expected precision: ${isNot ? ' ' : ''} ${(0,
  65. _jestMatcherUtils.stringify)(precision)}\n` +
  66. `Expected difference: ${isNot ? 'not ' : ''}< ${(0,
  67. _jestMatcherUtils.EXPECTED_COLOR)(expectedDiffString)}\n` +
  68. `Received difference: ${isNot ? ' ' : ''} ${(0,
  69. _jestMatcherUtils.RECEIVED_COLOR)(receivedDiffString)}`
  70. );
  71. };
  72. exports.printCloseTo = printCloseTo;
  73. const printExpectedConstructorName = (label, expected) =>
  74. printConstructorName(label, expected, false, true) + '\n';
  75. exports.printExpectedConstructorName = printExpectedConstructorName;
  76. const printExpectedConstructorNameNot = (label, expected) =>
  77. printConstructorName(label, expected, true, true) + '\n';
  78. exports.printExpectedConstructorNameNot = printExpectedConstructorNameNot;
  79. const printReceivedConstructorName = (label, received) =>
  80. printConstructorName(label, received, false, false) + '\n'; // Do not call function if received is equal to expected.
  81. exports.printReceivedConstructorName = printReceivedConstructorName;
  82. const printReceivedConstructorNameNot = (label, received, expected) =>
  83. typeof expected.name === 'string' &&
  84. expected.name.length !== 0 &&
  85. typeof received.name === 'string' &&
  86. received.name.length !== 0
  87. ? printConstructorName(label, received, true, false) +
  88. ` ${
  89. Object.getPrototypeOf(received) === expected
  90. ? 'extends'
  91. : 'extends … extends'
  92. } ${(0, _jestMatcherUtils.EXPECTED_COLOR)(expected.name)}` +
  93. '\n'
  94. : printConstructorName(label, received, false, false) + '\n';
  95. exports.printReceivedConstructorNameNot = printReceivedConstructorNameNot;
  96. const printConstructorName = (label, constructor, isNot, isExpected) =>
  97. typeof constructor.name !== 'string'
  98. ? `${label} name is not a string`
  99. : constructor.name.length === 0
  100. ? `${label} name is an empty string`
  101. : `${label}: ${!isNot ? '' : isExpected ? 'not ' : ' '}${
  102. isExpected
  103. ? (0, _jestMatcherUtils.EXPECTED_COLOR)(constructor.name)
  104. : (0, _jestMatcherUtils.RECEIVED_COLOR)(constructor.name)
  105. }`;