assert_support.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. var _jestMatcherUtils = require('jest-matcher-utils');
  6. var _chalk = require('chalk');
  7. var _chalk2 = _interopRequireDefault(_chalk);
  8. var _jestDiff = require('jest-diff');
  9. var _jestDiff2 = _interopRequireDefault(_jestDiff);
  10. function _interopRequireDefault(obj) {
  11. return obj && obj.__esModule ? obj : {default: obj};
  12. }
  13. /**
  14. * Copyright (c) 2017-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 assertOperatorsMap = {
  22. '!=': 'notEqual',
  23. '!==': 'notStrictEqual',
  24. '==': 'equal',
  25. '===': 'strictEqual'
  26. };
  27. const humanReadableOperators = {
  28. deepEqual: 'to deeply equal',
  29. deepStrictEqual: 'to deeply and strictly equal',
  30. equal: 'to be equal',
  31. notDeepEqual: 'not to deeply equal',
  32. notDeepStrictEqual: 'not to deeply and strictly equal',
  33. notEqual: 'to not be equal',
  34. notStrictEqual: 'not be strictly equal',
  35. strictEqual: 'to strictly be equal'
  36. };
  37. const getOperatorName = (operator, stack) => {
  38. if (typeof operator === 'string') {
  39. return assertOperatorsMap[operator] || operator;
  40. }
  41. if (stack.match('.doesNotThrow')) {
  42. return 'doesNotThrow';
  43. }
  44. if (stack.match('.throws')) {
  45. return 'throws';
  46. }
  47. return '';
  48. };
  49. const operatorMessage = operator => {
  50. const niceOperatorName = getOperatorName(operator, '');
  51. // $FlowFixMe: we default to the operator itself, so holes in the map doesn't matter
  52. const humanReadableOperator = humanReadableOperators[niceOperatorName];
  53. return typeof operator === 'string'
  54. ? `${humanReadableOperator || niceOperatorName} to:\n`
  55. : '';
  56. };
  57. const assertThrowingMatcherHint = operatorName =>
  58. _chalk2.default.dim('assert') +
  59. _chalk2.default.dim('.' + operatorName + '(') +
  60. _chalk2.default.red('function') +
  61. _chalk2.default.dim(')');
  62. const assertMatcherHint = (operator, operatorName) => {
  63. let message =
  64. _chalk2.default.dim('assert') +
  65. _chalk2.default.dim('.' + operatorName + '(') +
  66. _chalk2.default.red('received') +
  67. _chalk2.default.dim(', ') +
  68. _chalk2.default.green('expected') +
  69. _chalk2.default.dim(')');
  70. if (operator === '==') {
  71. message +=
  72. ' or ' +
  73. _chalk2.default.dim('assert') +
  74. _chalk2.default.dim('(') +
  75. _chalk2.default.red('received') +
  76. _chalk2.default.dim(') ');
  77. }
  78. return message;
  79. };
  80. function assertionErrorMessage(error, options) {
  81. const expected = error.expected,
  82. actual = error.actual,
  83. generatedMessage = error.generatedMessage,
  84. message = error.message,
  85. operator = error.operator,
  86. stack = error.stack;
  87. const diffString = (0, _jestDiff2.default)(expected, actual, options);
  88. const hasCustomMessage = !generatedMessage;
  89. const operatorName = getOperatorName(operator, stack);
  90. const trimmedStack = stack
  91. .replace(message, '')
  92. .replace(/AssertionError(.*)/g, '');
  93. if (operatorName === 'doesNotThrow') {
  94. return (
  95. assertThrowingMatcherHint(operatorName) +
  96. '\n\n' +
  97. _chalk2.default.reset(`Expected the function not to throw an error.\n`) +
  98. _chalk2.default.reset(`Instead, it threw:\n`) +
  99. ` ${(0, _jestMatcherUtils.printReceived)(actual)}` +
  100. _chalk2.default.reset(
  101. hasCustomMessage ? '\n\nMessage:\n ' + message : ''
  102. ) +
  103. trimmedStack
  104. );
  105. }
  106. if (operatorName === 'throws') {
  107. return (
  108. assertThrowingMatcherHint(operatorName) +
  109. '\n\n' +
  110. _chalk2.default.reset(`Expected the function to throw an error.\n`) +
  111. _chalk2.default.reset(`But it didn't throw anything.`) +
  112. _chalk2.default.reset(
  113. hasCustomMessage ? '\n\nMessage:\n ' + message : ''
  114. ) +
  115. trimmedStack
  116. );
  117. }
  118. return (
  119. assertMatcherHint(operator, operatorName) +
  120. '\n\n' +
  121. _chalk2.default.reset(`Expected value ${operatorMessage(operator)}`) +
  122. ` ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  123. _chalk2.default.reset(`Received:\n`) +
  124. ` ${(0, _jestMatcherUtils.printReceived)(actual)}` +
  125. _chalk2.default.reset(
  126. hasCustomMessage ? '\n\nMessage:\n ' + message : ''
  127. ) +
  128. (diffString ? `\n\nDifference:\n\n${diffString}` : '') +
  129. trimmedStack
  130. );
  131. }
  132. exports.default = assertionErrorMessage;