print.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.printReceivedConstructorNameNot = exports.printReceivedConstructorName = exports.printExpectedConstructorNameNot = exports.printExpectedConstructorName = 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. // Format substring but do not enclose in double quote marks.
  15. // The replacement is compatible with pretty-format package.
  16. const printSubstring = val => val.replace(/"|\\/g, '\\$&');
  17. const printReceivedStringContainExpectedSubstring = (received, start, length) =>
  18. (0, _jestMatcherUtils.RECEIVED_COLOR)(
  19. '"' +
  20. printSubstring(received.slice(0, start)) +
  21. (0, _jestMatcherUtils.INVERTED_COLOR)(
  22. printSubstring(received.slice(start, start + length))
  23. ) +
  24. printSubstring(received.slice(start + length)) +
  25. '"'
  26. );
  27. exports.printReceivedStringContainExpectedSubstring = printReceivedStringContainExpectedSubstring;
  28. const printReceivedStringContainExpectedResult = (received, result) =>
  29. result === null
  30. ? (0, _jestMatcherUtils.printReceived)(received)
  31. : printReceivedStringContainExpectedSubstring(
  32. received,
  33. result.index,
  34. result[0].length
  35. ); // The serialized array is compatible with pretty-format package min option.
  36. // However, items have default stringify depth (instead of depth - 1)
  37. // so expected item looks consistent by itself and enclosed in the array.
  38. exports.printReceivedStringContainExpectedResult = printReceivedStringContainExpectedResult;
  39. const printReceivedArrayContainExpectedItem = (received, index) =>
  40. (0, _jestMatcherUtils.RECEIVED_COLOR)(
  41. '[' +
  42. received
  43. .map((item, i) => {
  44. const stringified = (0, _jestMatcherUtils.stringify)(item);
  45. return i === index
  46. ? (0, _jestMatcherUtils.INVERTED_COLOR)(stringified)
  47. : stringified;
  48. })
  49. .join(', ') +
  50. ']'
  51. );
  52. exports.printReceivedArrayContainExpectedItem = printReceivedArrayContainExpectedItem;
  53. const printExpectedConstructorName = (label, expected) =>
  54. printConstructorName(label, expected, false, true) + '\n';
  55. exports.printExpectedConstructorName = printExpectedConstructorName;
  56. const printExpectedConstructorNameNot = (label, expected) =>
  57. printConstructorName(label, expected, true, true) + '\n';
  58. exports.printExpectedConstructorNameNot = printExpectedConstructorNameNot;
  59. const printReceivedConstructorName = (label, received) =>
  60. printConstructorName(label, received, false, false) + '\n'; // Do not call function if received is equal to expected.
  61. exports.printReceivedConstructorName = printReceivedConstructorName;
  62. const printReceivedConstructorNameNot = (label, received, expected) =>
  63. typeof expected.name === 'string' &&
  64. expected.name.length !== 0 &&
  65. typeof received.name === 'string' &&
  66. received.name.length !== 0
  67. ? printConstructorName(label, received, true, false) +
  68. ` ${
  69. Object.getPrototypeOf(received) === expected
  70. ? 'extends'
  71. : 'extends … extends'
  72. } ${(0, _jestMatcherUtils.EXPECTED_COLOR)(expected.name)}` +
  73. '\n'
  74. : printConstructorName(label, received, false, false) + '\n';
  75. exports.printReceivedConstructorNameNot = printReceivedConstructorNameNot;
  76. const printConstructorName = (label, constructor, isNot, isExpected) =>
  77. typeof constructor.name !== 'string'
  78. ? `${label} name is not a string`
  79. : constructor.name.length === 0
  80. ? `${label} name is an empty string`
  81. : `${label}: ${!isNot ? '' : isExpected ? 'not ' : ' '}${
  82. isExpected
  83. ? (0, _jestMatcherUtils.EXPECTED_COLOR)(constructor.name)
  84. : (0, _jestMatcherUtils.RECEIVED_COLOR)(constructor.name)
  85. }`;