assertionErrorMessage.js 4.3 KB

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