index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.matcherHint = exports.pluralize = exports.ensureNumbers = exports.ensureExpectedIsNumber = exports.ensureActualIsNumber = exports.ensureNoExpected = exports.printWithType = exports.printExpected = exports.printReceived = exports.highlightTrailingWhitespace = exports.stringify = exports.SUGGEST_TO_CONTAIN_EQUAL = exports.SUGGEST_TO_EQUAL = exports.RECEIVED_COLOR = exports.EXPECTED_COLOR = undefined;
  6. var _chalk = require('chalk');
  7. var _chalk2 = _interopRequireDefault(_chalk);
  8. var _jestGetType = require('jest-get-type');
  9. var _jestGetType2 = _interopRequireDefault(_jestGetType);
  10. var _prettyFormat = require('pretty-format');
  11. var _prettyFormat2 = _interopRequireDefault(_prettyFormat);
  12. function _interopRequireDefault(obj) {
  13. return obj && obj.__esModule ? obj : {default: obj};
  14. }
  15. var _prettyFormat$plugins = _prettyFormat2.default.plugins;
  16. /**
  17. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  18. *
  19. * This source code is licensed under the MIT license found in the
  20. * LICENSE file in the root directory of this source tree.
  21. *
  22. *
  23. */
  24. const AsymmetricMatcher = _prettyFormat$plugins.AsymmetricMatcher,
  25. DOMCollection = _prettyFormat$plugins.DOMCollection,
  26. DOMElement = _prettyFormat$plugins.DOMElement,
  27. Immutable = _prettyFormat$plugins.Immutable,
  28. ReactElement = _prettyFormat$plugins.ReactElement,
  29. ReactTestComponent = _prettyFormat$plugins.ReactTestComponent;
  30. const PLUGINS = [
  31. ReactTestComponent,
  32. ReactElement,
  33. DOMElement,
  34. DOMCollection,
  35. Immutable,
  36. AsymmetricMatcher
  37. ];
  38. const EXPECTED_COLOR = (exports.EXPECTED_COLOR = _chalk2.default.green);
  39. const RECEIVED_COLOR = (exports.RECEIVED_COLOR = _chalk2.default.red);
  40. const NUMBERS = [
  41. 'zero',
  42. 'one',
  43. 'two',
  44. 'three',
  45. 'four',
  46. 'five',
  47. 'six',
  48. 'seven',
  49. 'eight',
  50. 'nine',
  51. 'ten',
  52. 'eleven',
  53. 'twelve',
  54. 'thirteen'
  55. ];
  56. const SUGGEST_TO_EQUAL = (exports.SUGGEST_TO_EQUAL = _chalk2.default.dim(
  57. 'Note that you are testing for equality with the stricter `toBe` matcher using `Object.is`. For deep equality only, use `toEqual` instead.'
  58. ));
  59. const SUGGEST_TO_CONTAIN_EQUAL = (exports.SUGGEST_TO_CONTAIN_EQUAL = _chalk2.default.dim(
  60. 'Looks like you wanted to test for object/array equality with the stricter `toContain` matcher. You probably need to use `toContainEqual` instead.'
  61. ));
  62. const stringify = (exports.stringify = function(object) {
  63. let maxDepth =
  64. arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10;
  65. const MAX_LENGTH = 10000;
  66. let result;
  67. try {
  68. result = (0, _prettyFormat2.default)(object, {
  69. maxDepth: maxDepth,
  70. min: true,
  71. plugins: PLUGINS
  72. });
  73. } catch (e) {
  74. result = (0, _prettyFormat2.default)(object, {
  75. callToJSON: false,
  76. maxDepth: maxDepth,
  77. min: true,
  78. plugins: PLUGINS
  79. });
  80. }
  81. return result.length >= MAX_LENGTH && maxDepth > 1
  82. ? stringify(object, Math.floor(maxDepth / 2))
  83. : result;
  84. });
  85. const highlightTrailingWhitespace = (exports.highlightTrailingWhitespace = text =>
  86. text.replace(/\s+$/gm, _chalk2.default.inverse('$&')));
  87. const printReceived = (exports.printReceived = object =>
  88. RECEIVED_COLOR(highlightTrailingWhitespace(stringify(object))));
  89. const printExpected = (exports.printExpected = value =>
  90. EXPECTED_COLOR(highlightTrailingWhitespace(stringify(value))));
  91. const printWithType = (exports.printWithType = (name, received, print) => {
  92. const type = (0, _jestGetType2.default)(received);
  93. return (
  94. name +
  95. ':' +
  96. (type !== 'null' && type !== 'undefined' ? '\n ' + type + ': ' : ' ') +
  97. print(received)
  98. );
  99. });
  100. const ensureNoExpected = (exports.ensureNoExpected = (
  101. expected,
  102. matcherName
  103. ) => {
  104. matcherName || (matcherName = 'This');
  105. if (typeof expected !== 'undefined') {
  106. throw new Error(
  107. matcherHint('[.not]' + matcherName, undefined, '') +
  108. '\n\n' +
  109. 'Matcher does not accept any arguments.\n' +
  110. printWithType('Got', expected, printExpected)
  111. );
  112. }
  113. });
  114. const ensureActualIsNumber = (exports.ensureActualIsNumber = (
  115. actual,
  116. matcherName
  117. ) => {
  118. matcherName || (matcherName = 'This matcher');
  119. if (typeof actual !== 'number') {
  120. throw new Error(
  121. matcherHint('[.not]' + matcherName) +
  122. '\n\n' +
  123. `Received value must be a number.\n` +
  124. printWithType('Received', actual, printReceived)
  125. );
  126. }
  127. });
  128. const ensureExpectedIsNumber = (exports.ensureExpectedIsNumber = (
  129. expected,
  130. matcherName
  131. ) => {
  132. matcherName || (matcherName = 'This matcher');
  133. if (typeof expected !== 'number') {
  134. throw new Error(
  135. matcherHint('[.not]' + matcherName) +
  136. '\n\n' +
  137. `Expected value must be a number.\n` +
  138. printWithType('Got', expected, printExpected)
  139. );
  140. }
  141. });
  142. const ensureNumbers = (exports.ensureNumbers = (
  143. actual,
  144. expected,
  145. matcherName
  146. ) => {
  147. ensureActualIsNumber(actual, matcherName);
  148. ensureExpectedIsNumber(expected, matcherName);
  149. });
  150. const pluralize = (exports.pluralize = (word, count) =>
  151. (NUMBERS[count] || count) + ' ' + word + (count === 1 ? '' : 's'));
  152. const matcherHint = (exports.matcherHint = function(matcherName) {
  153. let received =
  154. arguments.length > 1 && arguments[1] !== undefined
  155. ? arguments[1]
  156. : 'received';
  157. let expected =
  158. arguments.length > 2 && arguments[2] !== undefined
  159. ? arguments[2]
  160. : 'expected';
  161. let options =
  162. arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  163. const comment = options.comment,
  164. isDirectExpectCall = options.isDirectExpectCall,
  165. isNot = options.isNot,
  166. secondArgument = options.secondArgument;
  167. return (
  168. _chalk2.default.dim('expect' + (isDirectExpectCall ? '' : '(')) +
  169. RECEIVED_COLOR(received) +
  170. (isNot
  171. ? `${_chalk2.default.dim(').')}not${_chalk2.default.dim(
  172. matcherName + '('
  173. )}`
  174. : _chalk2.default.dim(
  175. (isDirectExpectCall ? '' : ')') + matcherName + '('
  176. )) +
  177. EXPECTED_COLOR(expected) +
  178. (secondArgument
  179. ? `${_chalk2.default.dim(', ')}${EXPECTED_COLOR(secondArgument)}`
  180. : '') +
  181. _chalk2.default.dim(`)${comment ? ` // ${comment}` : ''}`)
  182. );
  183. });