index.js 6.8 KB

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