assert_support.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. /**
  12. * Copyright (c) 2017-present, Facebook, Inc. 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. *
  18. */
  19. const assertOperatorsMap = {
  20. '!=': 'notEqual',
  21. '!==': 'notStrictEqual',
  22. '==': 'equal',
  23. '===': 'strictEqual'
  24. };
  25. const humanReadableOperators = {
  26. deepEqual: 'to deeply equal',
  27. deepStrictEqual: 'to deeply and strictly equal',
  28. equal: 'to be equal',
  29. notDeepEqual: 'not to deeply equal',
  30. notDeepStrictEqual: 'not to deeply and strictly equal',
  31. notEqual: 'to not be equal',
  32. notStrictEqual: 'not be strictly equal',
  33. strictEqual: 'to strictly be equal'
  34. };
  35. const getOperatorName = (operator, stack) => {
  36. if (typeof operator === 'string') {
  37. return assertOperatorsMap[operator] || operator;
  38. }
  39. if (stack.match('.doesNotThrow')) {
  40. return 'doesNotThrow';
  41. }
  42. if (stack.match('.throws')) {
  43. return 'throws';
  44. }
  45. return '';
  46. };
  47. const operatorMessage = operator => {
  48. const niceOperatorName = getOperatorName(operator, '');
  49. // $FlowFixMe: we default to the operator itself, so holes in the map doesn't matter
  50. const humanReadableOperator = humanReadableOperators[niceOperatorName];
  51. return typeof operator === 'string' ? `${humanReadableOperator || niceOperatorName} to:\n` : '';
  52. };
  53. const assertThrowingMatcherHint = operatorName => {
  54. return _chalk2.default.dim('assert') + _chalk2.default.dim('.' + operatorName + '(') + _chalk2.default.red('function') + _chalk2.default.dim(')');
  55. };
  56. const assertMatcherHint = (operator, operatorName) => {
  57. let message = _chalk2.default.dim('assert') + _chalk2.default.dim('.' + operatorName + '(') + _chalk2.default.red('received') + _chalk2.default.dim(', ') + _chalk2.default.green('expected') + _chalk2.default.dim(')');
  58. if (operator === '==') {
  59. message += ' or ' + _chalk2.default.dim('assert') + _chalk2.default.dim('(') + _chalk2.default.red('received') + _chalk2.default.dim(') ');
  60. }
  61. return message;
  62. };
  63. function assertionErrorMessage(error, options) {
  64. const expected = error.expected,
  65. actual = error.actual,
  66. generatedMessage = error.generatedMessage,
  67. message = error.message,
  68. operator = error.operator,
  69. stack = error.stack;
  70. const diffString = (0, _jestDiff2.default)(expected, actual, options);
  71. const hasCustomMessage = !generatedMessage;
  72. const operatorName = getOperatorName(operator, stack);
  73. const trimmedStack = stack.replace(message, '').replace(/AssertionError(.*)/g, '');
  74. if (operatorName === 'doesNotThrow') {
  75. return assertThrowingMatcherHint(operatorName) + '\n\n' + _chalk2.default.reset(`Expected the function not to throw an error.\n`) + _chalk2.default.reset(`Instead, it threw:\n`) + ` ${(0, _jestMatcherUtils.printReceived)(actual)}` + _chalk2.default.reset(hasCustomMessage ? '\n\nMessage:\n ' + message : '') + trimmedStack;
  76. }
  77. if (operatorName === 'throws') {
  78. return assertThrowingMatcherHint(operatorName) + '\n\n' + _chalk2.default.reset(`Expected the function to throw an error.\n`) + _chalk2.default.reset(`But it didn't throw anything.`) + _chalk2.default.reset(hasCustomMessage ? '\n\nMessage:\n ' + message : '') + trimmedStack;
  79. }
  80. return assertMatcherHint(operator, operatorName) + '\n\n' + _chalk2.default.reset(`Expected value ${operatorMessage(operator)}`) + ` ${(0, _jestMatcherUtils.printExpected)(expected)}\n` + _chalk2.default.reset(`Received:\n`) + ` ${(0, _jestMatcherUtils.printReceived)(actual)}` + _chalk2.default.reset(hasCustomMessage ? '\n\nMessage:\n ' + message : '') + (diffString ? `\n\nDifference:\n\n${diffString}` : '') + trimmedStack;
  81. }
  82. exports.default = assertionErrorMessage;