index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. Object.defineProperty(exports, 'DIFF_DELETE', {
  6. enumerable: true,
  7. get: function () {
  8. return _cleanupSemantic.DIFF_DELETE;
  9. }
  10. });
  11. Object.defineProperty(exports, 'DIFF_EQUAL', {
  12. enumerable: true,
  13. get: function () {
  14. return _cleanupSemantic.DIFF_EQUAL;
  15. }
  16. });
  17. Object.defineProperty(exports, 'DIFF_INSERT', {
  18. enumerable: true,
  19. get: function () {
  20. return _cleanupSemantic.DIFF_INSERT;
  21. }
  22. });
  23. Object.defineProperty(exports, 'Diff', {
  24. enumerable: true,
  25. get: function () {
  26. return _cleanupSemantic.Diff;
  27. }
  28. });
  29. Object.defineProperty(exports, 'diffLinesRaw', {
  30. enumerable: true,
  31. get: function () {
  32. return _diffLines.diffLinesRaw;
  33. }
  34. });
  35. Object.defineProperty(exports, 'diffLinesUnified', {
  36. enumerable: true,
  37. get: function () {
  38. return _diffLines.diffLinesUnified;
  39. }
  40. });
  41. Object.defineProperty(exports, 'diffLinesUnified2', {
  42. enumerable: true,
  43. get: function () {
  44. return _diffLines.diffLinesUnified2;
  45. }
  46. });
  47. Object.defineProperty(exports, 'diffStringsRaw', {
  48. enumerable: true,
  49. get: function () {
  50. return _printDiffs.diffStringsRaw;
  51. }
  52. });
  53. Object.defineProperty(exports, 'diffStringsUnified', {
  54. enumerable: true,
  55. get: function () {
  56. return _printDiffs.diffStringsUnified;
  57. }
  58. });
  59. exports.default = void 0;
  60. var _chalk = _interopRequireDefault(require('chalk'));
  61. var _jestGetType = _interopRequireDefault(require('jest-get-type'));
  62. var _prettyFormat = _interopRequireDefault(require('pretty-format'));
  63. var _cleanupSemantic = require('./cleanupSemantic');
  64. var _constants = require('./constants');
  65. var _diffLines = require('./diffLines');
  66. var _normalizeDiffOptions = require('./normalizeDiffOptions');
  67. var _printDiffs = require('./printDiffs');
  68. function _interopRequireDefault(obj) {
  69. return obj && obj.__esModule ? obj : {default: obj};
  70. }
  71. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  72. const getCommonMessage = (message, options) => {
  73. const {commonColor} = (0, _normalizeDiffOptions.normalizeDiffOptions)(
  74. options
  75. );
  76. return commonColor(message);
  77. };
  78. const {
  79. AsymmetricMatcher,
  80. DOMCollection,
  81. DOMElement,
  82. Immutable,
  83. ReactElement,
  84. ReactTestComponent
  85. } = _prettyFormat.default.plugins;
  86. const PLUGINS = [
  87. ReactTestComponent,
  88. ReactElement,
  89. DOMElement,
  90. DOMCollection,
  91. Immutable,
  92. AsymmetricMatcher
  93. ];
  94. const FORMAT_OPTIONS = {
  95. plugins: PLUGINS
  96. };
  97. const FORMAT_OPTIONS_0 = {...FORMAT_OPTIONS, indent: 0};
  98. const FALLBACK_FORMAT_OPTIONS = {
  99. callToJSON: false,
  100. maxDepth: 10,
  101. plugins: PLUGINS
  102. };
  103. const FALLBACK_FORMAT_OPTIONS_0 = {...FALLBACK_FORMAT_OPTIONS, indent: 0}; // Generate a string that will highlight the difference between two values
  104. // with green and red. (similar to how github does code diffing)
  105. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  106. function diff(a, b, options) {
  107. if (Object.is(a, b)) {
  108. return getCommonMessage(_constants.NO_DIFF_MESSAGE, options);
  109. }
  110. const aType = (0, _jestGetType.default)(a);
  111. let expectedType = aType;
  112. let omitDifference = false;
  113. if (aType === 'object' && typeof a.asymmetricMatch === 'function') {
  114. if (a.$$typeof !== Symbol.for('jest.asymmetricMatcher')) {
  115. // Do not know expected type of user-defined asymmetric matcher.
  116. return null;
  117. }
  118. if (typeof a.getExpectedType !== 'function') {
  119. // For example, expect.anything() matches either null or undefined
  120. return null;
  121. }
  122. expectedType = a.getExpectedType(); // Primitive types boolean and number omit difference below.
  123. // For example, omit difference for expect.stringMatching(regexp)
  124. omitDifference = expectedType === 'string';
  125. }
  126. if (expectedType !== (0, _jestGetType.default)(b)) {
  127. return (
  128. ' Comparing two different types of values.' +
  129. ` Expected ${_chalk.default.green(expectedType)} but ` +
  130. `received ${_chalk.default.red((0, _jestGetType.default)(b))}.`
  131. );
  132. }
  133. if (omitDifference) {
  134. return null;
  135. }
  136. switch (aType) {
  137. case 'string':
  138. return (0, _diffLines.diffLinesUnified)(
  139. a.split('\n'),
  140. b.split('\n'),
  141. options
  142. );
  143. case 'boolean':
  144. case 'number':
  145. return comparePrimitive(a, b, options);
  146. case 'map':
  147. return compareObjects(sortMap(a), sortMap(b), options);
  148. case 'set':
  149. return compareObjects(sortSet(a), sortSet(b), options);
  150. default:
  151. return compareObjects(a, b, options);
  152. }
  153. }
  154. function comparePrimitive(a, b, options) {
  155. const aFormat = (0, _prettyFormat.default)(a, FORMAT_OPTIONS);
  156. const bFormat = (0, _prettyFormat.default)(b, FORMAT_OPTIONS);
  157. return aFormat === bFormat
  158. ? getCommonMessage(_constants.NO_DIFF_MESSAGE, options)
  159. : (0, _diffLines.diffLinesUnified)(
  160. aFormat.split('\n'),
  161. bFormat.split('\n'),
  162. options
  163. );
  164. }
  165. function sortMap(map) {
  166. return new Map(Array.from(map.entries()).sort());
  167. }
  168. function sortSet(set) {
  169. return new Set(Array.from(set.values()).sort());
  170. }
  171. function compareObjects(a, b, options) {
  172. let difference;
  173. let hasThrown = false;
  174. const noDiffMessage = getCommonMessage(_constants.NO_DIFF_MESSAGE, options);
  175. try {
  176. const aCompare = (0, _prettyFormat.default)(a, FORMAT_OPTIONS_0);
  177. const bCompare = (0, _prettyFormat.default)(b, FORMAT_OPTIONS_0);
  178. if (aCompare === bCompare) {
  179. difference = noDiffMessage;
  180. } else {
  181. const aDisplay = (0, _prettyFormat.default)(a, FORMAT_OPTIONS);
  182. const bDisplay = (0, _prettyFormat.default)(b, FORMAT_OPTIONS);
  183. difference = (0, _diffLines.diffLinesUnified2)(
  184. aDisplay.split('\n'),
  185. bDisplay.split('\n'),
  186. aCompare.split('\n'),
  187. bCompare.split('\n'),
  188. options
  189. );
  190. }
  191. } catch {
  192. hasThrown = true;
  193. } // If the comparison yields no results, compare again but this time
  194. // without calling `toJSON`. It's also possible that toJSON might throw.
  195. if (difference === undefined || difference === noDiffMessage) {
  196. const aCompare = (0, _prettyFormat.default)(a, FALLBACK_FORMAT_OPTIONS_0);
  197. const bCompare = (0, _prettyFormat.default)(b, FALLBACK_FORMAT_OPTIONS_0);
  198. if (aCompare === bCompare) {
  199. difference = noDiffMessage;
  200. } else {
  201. const aDisplay = (0, _prettyFormat.default)(a, FALLBACK_FORMAT_OPTIONS);
  202. const bDisplay = (0, _prettyFormat.default)(b, FALLBACK_FORMAT_OPTIONS);
  203. difference = (0, _diffLines.diffLinesUnified2)(
  204. aDisplay.split('\n'),
  205. bDisplay.split('\n'),
  206. aCompare.split('\n'),
  207. bCompare.split('\n'),
  208. options
  209. );
  210. }
  211. if (difference !== noDiffMessage && !hasThrown) {
  212. difference =
  213. getCommonMessage(_constants.SIMILAR_MESSAGE, options) +
  214. '\n\n' +
  215. difference;
  216. }
  217. }
  218. return difference;
  219. }
  220. var _default = diff;
  221. exports.default = _default;