assertionErrorMessage.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _chalk = _interopRequireDefault(require('chalk'));
  7. var _jestMatcherUtils = require('jest-matcher-utils');
  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. } // this fallback is only needed for versions older than node 10
  43. if (stack.match('.fail')) {
  44. return 'fail';
  45. }
  46. return '';
  47. };
  48. const operatorMessage = operator => {
  49. const niceOperatorName = getOperatorName(operator, '');
  50. const humanReadableOperator = humanReadableOperators[niceOperatorName];
  51. return typeof operator === 'string'
  52. ? `${humanReadableOperator || niceOperatorName} to:\n`
  53. : '';
  54. };
  55. const assertThrowingMatcherHint = operatorName =>
  56. operatorName
  57. ? _chalk.default.dim('assert') +
  58. _chalk.default.dim('.' + operatorName + '(') +
  59. _chalk.default.red('function') +
  60. _chalk.default.dim(')')
  61. : '';
  62. const assertMatcherHint = (operator, operatorName, expected) => {
  63. let message = '';
  64. if (operator === '==' && expected === true) {
  65. message =
  66. _chalk.default.dim('assert') +
  67. _chalk.default.dim('(') +
  68. _chalk.default.red('received') +
  69. _chalk.default.dim(')');
  70. } else if (operatorName) {
  71. message =
  72. _chalk.default.dim('assert') +
  73. _chalk.default.dim('.' + operatorName + '(') +
  74. _chalk.default.red('received') +
  75. _chalk.default.dim(', ') +
  76. _chalk.default.green('expected') +
  77. _chalk.default.dim(')');
  78. }
  79. return message;
  80. };
  81. function assertionErrorMessage(error, options) {
  82. const {expected, actual, generatedMessage, message, operator, stack} = error;
  83. const diffString = (0, _jestMatcherUtils.diff)(expected, actual, options);
  84. const hasCustomMessage = !generatedMessage;
  85. const operatorName = getOperatorName(operator, stack);
  86. const trimmedStack = stack
  87. .replace(message, '')
  88. .replace(/AssertionError(.*)/g, '');
  89. if (operatorName === 'doesNotThrow') {
  90. return (
  91. buildHintString(assertThrowingMatcherHint(operatorName)) +
  92. _chalk.default.reset(`Expected the function not to throw an error.\n`) +
  93. _chalk.default.reset(`Instead, it threw:\n`) +
  94. ` ${(0, _jestMatcherUtils.printReceived)(actual)}` +
  95. _chalk.default.reset(
  96. hasCustomMessage ? '\n\nMessage:\n ' + message : ''
  97. ) +
  98. trimmedStack
  99. );
  100. }
  101. if (operatorName === 'throws') {
  102. return (
  103. buildHintString(assertThrowingMatcherHint(operatorName)) +
  104. _chalk.default.reset(`Expected the function to throw an error.\n`) +
  105. _chalk.default.reset(`But it didn't throw anything.`) +
  106. _chalk.default.reset(
  107. hasCustomMessage ? '\n\nMessage:\n ' + message : ''
  108. ) +
  109. trimmedStack
  110. );
  111. }
  112. if (operatorName === 'fail') {
  113. return (
  114. buildHintString(assertMatcherHint(operator, operatorName, expected)) +
  115. _chalk.default.reset(hasCustomMessage ? 'Message:\n ' + message : '') +
  116. trimmedStack
  117. );
  118. }
  119. return (
  120. buildHintString(assertMatcherHint(operator, operatorName, expected)) +
  121. _chalk.default.reset(`Expected value ${operatorMessage(operator)}`) +
  122. ` ${(0, _jestMatcherUtils.printExpected)(expected)}\n` +
  123. _chalk.default.reset(`Received:\n`) +
  124. ` ${(0, _jestMatcherUtils.printReceived)(actual)}` +
  125. _chalk.default.reset(hasCustomMessage ? '\n\nMessage:\n ' + message : '') +
  126. (diffString ? `\n\nDifference:\n\n${diffString}` : '') +
  127. trimmedStack
  128. );
  129. }
  130. function buildHintString(hint) {
  131. return hint ? hint + '\n\n' : '';
  132. }
  133. var _default = assertionErrorMessage;
  134. exports.default = _default;