to_throw_matchers.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createMatcher = undefined;
  6. var _jestGetType = require('jest-get-type');
  7. var _jestGetType2 = _interopRequireDefault(_jestGetType);
  8. var _jestRegexUtil = require('jest-regex-util');
  9. var _jestMessageUtil = require('jest-message-util');
  10. var _jestMatcherUtils = require('jest-matcher-utils');
  11. var _jasmine_utils = require('./jasmine_utils');
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. /**
  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 createMatcher = exports.createMatcher = (matcherName, fromPromise) => (actual, expected) => {
  22. const value = expected;
  23. let error;
  24. if (fromPromise) {
  25. error = actual;
  26. } else {
  27. if (typeof actual !== 'function') {
  28. throw new Error((0, _jestMatcherUtils.matcherHint)(matcherName, 'function', (0, _jestGetType2.default)(value)) + '\n\n' + 'Received value must be a function, but instead ' + `"${(0, _jestGetType2.default)(actual)}" was found`);
  29. }
  30. try {
  31. actual();
  32. } catch (e) {
  33. error = e;
  34. }
  35. }
  36. if (typeof expected === 'string') {
  37. expected = new RegExp((0, _jestRegexUtil.escapeStrForRegex)(expected));
  38. }
  39. if (typeof expected === 'function') {
  40. return toThrowMatchingError(matcherName, error, expected);
  41. } else if (expected instanceof RegExp) {
  42. return toThrowMatchingStringOrRegexp(matcherName, error, expected, value);
  43. } else if (expected && typeof expected === 'object') {
  44. return toThrowMatchingErrorInstance(matcherName, error, expected);
  45. } else if (expected === undefined) {
  46. const pass = error !== undefined;
  47. return {
  48. message: pass ? () => (0, _jestMatcherUtils.matcherHint)('.not' + matcherName, 'function', '') + '\n\n' + 'Expected the function not to throw an error.\n' + printActualErrorMessage(error) : () => (0, _jestMatcherUtils.matcherHint)(matcherName, 'function', (0, _jestGetType2.default)(value)) + '\n\n' + 'Expected the function to throw an error.\n' + printActualErrorMessage(error),
  49. pass
  50. };
  51. } else {
  52. throw new Error((0, _jestMatcherUtils.matcherHint)('.not' + matcherName, 'function', (0, _jestGetType2.default)(value)) + '\n\n' + 'Unexpected argument passed.\nExpected: ' + `${(0, _jestMatcherUtils.printExpected)('string')}, ${(0, _jestMatcherUtils.printExpected)('Error (type)')} or ${(0, _jestMatcherUtils.printExpected)('regexp')}.\n` + (0, _jestMatcherUtils.printWithType)('Got', String(expected), _jestMatcherUtils.printExpected));
  53. }
  54. };
  55. const matchers = {
  56. toThrow: createMatcher('.toThrow'),
  57. toThrowError: createMatcher('.toThrowError')
  58. };
  59. const toThrowMatchingStringOrRegexp = (name, error, pattern, value) => {
  60. if (error && !error.message && !error.name) {
  61. error = new Error(error);
  62. }
  63. const pass = !!(error && error.message.match(pattern));
  64. const message = pass ? () => (0, _jestMatcherUtils.matcherHint)('.not' + name, 'function', (0, _jestGetType2.default)(value)) + '\n\n' + `Expected the function not to throw an error matching:\n` + ` ${(0, _jestMatcherUtils.printExpected)(value)}\n` + printActualErrorMessage(error) : () => (0, _jestMatcherUtils.matcherHint)(name, 'function', (0, _jestGetType2.default)(value)) + '\n\n' + `Expected the function to throw an error matching:\n` + ` ${(0, _jestMatcherUtils.printExpected)(value)}\n` + printActualErrorMessage(error);
  65. return { message, pass };
  66. };
  67. const toThrowMatchingErrorInstance = (name, error, expectedError) => {
  68. if (error && !error.message && !error.name) {
  69. error = new Error(error);
  70. }
  71. const pass = (0, _jasmine_utils.equals)(error, expectedError);
  72. const message = pass ? () => (0, _jestMatcherUtils.matcherHint)('.not' + name, 'function', 'error') + '\n\n' + `Expected the function not to throw an error matching:\n` + ` ${(0, _jestMatcherUtils.printExpected)(expectedError)}\n` + printActualErrorMessage(error) : () => (0, _jestMatcherUtils.matcherHint)(name, 'function', 'error') + '\n\n' + `Expected the function to throw an error matching:\n` + ` ${(0, _jestMatcherUtils.printExpected)(expectedError)}\n` + printActualErrorMessage(error);
  73. return { message, pass };
  74. };
  75. const toThrowMatchingError = (name, error, ErrorClass) => {
  76. const pass = !!(error && error instanceof ErrorClass);
  77. const message = pass ? () => (0, _jestMatcherUtils.matcherHint)('.not' + name, 'function', 'type') + '\n\n' + `Expected the function not to throw an error of type:\n` + ` ${(0, _jestMatcherUtils.printExpected)(ErrorClass.name)}\n` + printActualErrorMessage(error) : () => (0, _jestMatcherUtils.matcherHint)(name, 'function', 'type') + '\n\n' + `Expected the function to throw an error of type:\n` + ` ${(0, _jestMatcherUtils.printExpected)(ErrorClass.name)}\n` + printActualErrorMessage(error);
  78. return { message, pass };
  79. };
  80. const printActualErrorMessage = error => {
  81. if (error) {
  82. var _separateMessageFromS = (0, _jestMessageUtil.separateMessageFromStack)(error.stack);
  83. const message = _separateMessageFromS.message,
  84. stack = _separateMessageFromS.stack;
  85. return `Instead, it threw:\n` + (0, _jestMatcherUtils.RECEIVED_COLOR)(' ' + (0, _jestMatcherUtils.highlightTrailingWhitespace)(message) + (0, _jestMessageUtil.formatStackTrace)(stack, {
  86. rootDir: process.cwd(),
  87. testMatch: []
  88. }, {
  89. noStackTrace: false
  90. }));
  91. }
  92. return `But it didn't throw anything.`;
  93. };
  94. exports.default = matchers;