index.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_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) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. var _prettyFormat$plugins = _prettyFormat2.default.plugins; /**
  14. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  15. *
  16. * This source code is licensed under the MIT license found in the
  17. * LICENSE file in the root directory of this source tree.
  18. *
  19. *
  20. */
  21. const AsymmetricMatcher = _prettyFormat$plugins.AsymmetricMatcher,
  22. DOMCollection = _prettyFormat$plugins.DOMCollection,
  23. DOMElement = _prettyFormat$plugins.DOMElement,
  24. Immutable = _prettyFormat$plugins.Immutable,
  25. ReactElement = _prettyFormat$plugins.ReactElement,
  26. ReactTestComponent = _prettyFormat$plugins.ReactTestComponent;
  27. const PLUGINS = [ReactTestComponent, ReactElement, DOMElement, DOMCollection, Immutable, AsymmetricMatcher];
  28. const EXPECTED_COLOR = exports.EXPECTED_COLOR = _chalk2.default.green;
  29. const RECEIVED_COLOR = exports.RECEIVED_COLOR = _chalk2.default.red;
  30. const NUMBERS = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen'];
  31. const SUGGEST_TO_EQUAL = exports.SUGGEST_TO_EQUAL = _chalk2.default.dim('Looks like you wanted to test for object/array equality with strict `toBe` matcher. You probably need to use `toEqual` instead.');
  32. const stringify = exports.stringify = function (object) {
  33. let maxDepth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10;
  34. const MAX_LENGTH = 10000;
  35. let result;
  36. try {
  37. result = (0, _prettyFormat2.default)(object, {
  38. maxDepth,
  39. min: true,
  40. plugins: PLUGINS
  41. });
  42. } catch (e) {
  43. result = (0, _prettyFormat2.default)(object, {
  44. callToJSON: false,
  45. maxDepth,
  46. min: true,
  47. plugins: PLUGINS
  48. });
  49. }
  50. return result.length >= MAX_LENGTH && maxDepth > 1 ? stringify(object, Math.floor(maxDepth / 2)) : result;
  51. };
  52. const highlightTrailingWhitespace = exports.highlightTrailingWhitespace = text => text.replace(/\s+$/gm, _chalk2.default.inverse('$&'));
  53. const printReceived = exports.printReceived = object => RECEIVED_COLOR(highlightTrailingWhitespace(stringify(object)));
  54. const printExpected = exports.printExpected = value => EXPECTED_COLOR(highlightTrailingWhitespace(stringify(value)));
  55. const printWithType = exports.printWithType = (name, received, print) => {
  56. const type = (0, _jestGetType2.default)(received);
  57. return name + ':' + (type !== 'null' && type !== 'undefined' ? '\n ' + type + ': ' : ' ') + print(received);
  58. };
  59. const ensureNoExpected = exports.ensureNoExpected = (expected, matcherName) => {
  60. matcherName || (matcherName = 'This');
  61. if (typeof expected !== 'undefined') {
  62. throw new Error(matcherHint('[.not]' + matcherName, undefined, '') + '\n\n' + 'Matcher does not accept any arguments.\n' + printWithType('Got', expected, printExpected));
  63. }
  64. };
  65. const ensureActualIsNumber = exports.ensureActualIsNumber = (actual, matcherName) => {
  66. matcherName || (matcherName = 'This matcher');
  67. if (typeof actual !== 'number') {
  68. throw new Error(matcherHint('[.not]' + matcherName) + '\n\n' + `Received value must be a number.\n` + printWithType('Received', actual, printReceived));
  69. }
  70. };
  71. const ensureExpectedIsNumber = exports.ensureExpectedIsNumber = (expected, matcherName) => {
  72. matcherName || (matcherName = 'This matcher');
  73. if (typeof expected !== 'number') {
  74. throw new Error(matcherHint('[.not]' + matcherName) + '\n\n' + `Expected value must be a number.\n` + printWithType('Got', expected, printExpected));
  75. }
  76. };
  77. const ensureNumbers = exports.ensureNumbers = (actual, expected, matcherName) => {
  78. ensureActualIsNumber(actual, matcherName);
  79. ensureExpectedIsNumber(expected, matcherName);
  80. };
  81. const pluralize = exports.pluralize = (word, count) => (NUMBERS[count] || count) + ' ' + word + (count === 1 ? '' : 's');
  82. const matcherHint = exports.matcherHint = function (matcherName) {
  83. let received = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'received';
  84. let expected = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'expected';
  85. let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  86. const comment = options.comment,
  87. isDirectExpectCall = options.isDirectExpectCall,
  88. secondArgument = options.secondArgument;
  89. return _chalk2.default.dim('expect' + (isDirectExpectCall ? '' : '(')) + RECEIVED_COLOR(received) + _chalk2.default.dim((isDirectExpectCall ? '' : ')') + matcherName + '(') + EXPECTED_COLOR(expected) + (secondArgument ? `${_chalk2.default.dim(', ')}${EXPECTED_COLOR(secondArgument)}` : '') + _chalk2.default.dim(`)${comment ? ` // ${comment}` : ''}`);
  90. };