print.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.printDiffOrStringified = void 0;
  6. var _jestDiff = _interopRequireWildcard(require('jest-diff'));
  7. var _jestGetType = _interopRequireWildcard(require('jest-get-type'));
  8. var _jestMatcherUtils = require('jest-matcher-utils');
  9. var _prettyFormat = _interopRequireDefault(require('pretty-format'));
  10. var _utils = require('./utils');
  11. function _interopRequireDefault(obj) {
  12. return obj && obj.__esModule ? obj : {default: obj};
  13. }
  14. function _interopRequireWildcard(obj) {
  15. if (obj && obj.__esModule) {
  16. return obj;
  17. } else {
  18. var newObj = {};
  19. if (obj != null) {
  20. for (var key in obj) {
  21. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  22. var desc =
  23. Object.defineProperty && Object.getOwnPropertyDescriptor
  24. ? Object.getOwnPropertyDescriptor(obj, key)
  25. : {};
  26. if (desc.get || desc.set) {
  27. Object.defineProperty(newObj, key, desc);
  28. } else {
  29. newObj[key] = obj[key];
  30. }
  31. }
  32. }
  33. }
  34. newObj.default = obj;
  35. return newObj;
  36. }
  37. }
  38. /**
  39. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  40. *
  41. * This source code is licensed under the MIT license found in the
  42. * LICENSE file in the root directory of this source tree.
  43. */
  44. const isLineDiffable = received => {
  45. const receivedType = (0, _jestGetType.default)(received);
  46. if ((0, _jestGetType.isPrimitive)(received)) {
  47. return typeof received === 'string' && received.includes('\n');
  48. }
  49. if (
  50. receivedType === 'date' ||
  51. receivedType === 'function' ||
  52. receivedType === 'regexp'
  53. ) {
  54. return false;
  55. }
  56. if (received instanceof Error) {
  57. return false;
  58. }
  59. if (
  60. receivedType === 'object' &&
  61. typeof received.asymmetricMatch === 'function'
  62. ) {
  63. return false;
  64. }
  65. return true;
  66. };
  67. const printDiffOrStringified = (
  68. expectedSerializedTrimmed,
  69. receivedSerializedTrimmed,
  70. received,
  71. expectedLabel,
  72. receivedLabel,
  73. expand
  74. ) => {
  75. if (typeof received === 'string') {
  76. if (
  77. expectedSerializedTrimmed.length >= 2 &&
  78. expectedSerializedTrimmed.startsWith('"') &&
  79. expectedSerializedTrimmed.endsWith('"') &&
  80. receivedSerializedTrimmed ===
  81. (0, _utils.unescape)((0, _prettyFormat.default)(received))
  82. ) {
  83. // The expected snapshot looks like a stringified string.
  84. // The received serialization is default stringified string.
  85. // Undo default serialization of expected snapshot:
  86. // Remove enclosing double quote marks.
  87. // Remove backslash escape preceding backslash here,
  88. // because unescape replaced it only preceding double quote mark.
  89. return (0, _jestMatcherUtils.printDiffOrStringify)(
  90. expectedSerializedTrimmed.slice(1, -1).replace(/\\\\/g, '\\'),
  91. received,
  92. expectedLabel,
  93. receivedLabel,
  94. expand
  95. );
  96. } // Display substring highlight even when strings have custom serialization.
  97. const result = (0, _jestDiff.getStringDiff)(
  98. expectedSerializedTrimmed,
  99. receivedSerializedTrimmed,
  100. {
  101. aAnnotation: expectedLabel,
  102. bAnnotation: receivedLabel,
  103. expand
  104. }
  105. );
  106. if (result !== null) {
  107. if (result.isMultiline) {
  108. return result.annotatedDiff;
  109. } // Because not default stringify, call EXPECTED_COLOR and RECEIVED_COLOR
  110. // This is reason to call getStringDiff instead of printDiffOrStringify
  111. // Because there is no closing double quote mark at end of single lines,
  112. // future improvement is to call replaceSpacesAtEnd if it becomes public.
  113. const printLabel = (0, _jestMatcherUtils.getLabelPrinter)(
  114. expectedLabel,
  115. receivedLabel
  116. );
  117. return (
  118. printLabel(expectedLabel) +
  119. (0, _jestMatcherUtils.EXPECTED_COLOR)(result.a) +
  120. '\n' +
  121. printLabel(receivedLabel) +
  122. (0, _jestMatcherUtils.RECEIVED_COLOR)(result.b)
  123. );
  124. }
  125. }
  126. if (
  127. (expectedSerializedTrimmed.includes('\n') ||
  128. receivedSerializedTrimmed.includes('\n')) &&
  129. isLineDiffable(received)
  130. ) {
  131. return (0, _jestDiff.default)(
  132. expectedSerializedTrimmed,
  133. receivedSerializedTrimmed,
  134. {
  135. aAnnotation: expectedLabel,
  136. bAnnotation: receivedLabel,
  137. expand
  138. }
  139. );
  140. }
  141. const printLabel = (0, _jestMatcherUtils.getLabelPrinter)(
  142. expectedLabel,
  143. receivedLabel
  144. );
  145. return (
  146. printLabel(expectedLabel) +
  147. (0, _jestMatcherUtils.EXPECTED_COLOR)(expectedSerializedTrimmed) +
  148. '\n' +
  149. printLabel(receivedLabel) +
  150. (0, _jestMatcherUtils.RECEIVED_COLOR)(receivedSerializedTrimmed)
  151. );
  152. };
  153. exports.printDiffOrStringified = printDiffOrStringified;