to_throw_matchers.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. var _utils = require('./utils');
  13. function _interopRequireDefault(obj) {
  14. return obj && obj.__esModule ? obj : {default: obj};
  15. }
  16. const createMatcher = (exports.createMatcher = (matcherName, fromPromise) => (
  17. actual,
  18. expected
  19. ) => {
  20. const value = expected;
  21. let error;
  22. if (fromPromise && (0, _utils.isError)(actual)) {
  23. error = actual;
  24. } else {
  25. if (typeof actual !== 'function') {
  26. if (!fromPromise) {
  27. throw new Error(
  28. (0, _jestMatcherUtils.matcherHint)(
  29. matcherName,
  30. 'function',
  31. (0, _jestGetType2.default)(value)
  32. ) +
  33. '\n\n' +
  34. 'Received value must be a function, but instead ' +
  35. `"${(0, _jestGetType2.default)(actual)}" was found`
  36. );
  37. }
  38. } else {
  39. try {
  40. actual();
  41. } catch (e) {
  42. error = e;
  43. }
  44. }
  45. }
  46. if (typeof expected === 'string') {
  47. expected = new RegExp((0, _jestRegexUtil.escapeStrForRegex)(expected));
  48. }
  49. if (typeof expected === 'function') {
  50. return toThrowMatchingError(matcherName, error, expected);
  51. } else if (expected && typeof expected.test === 'function') {
  52. return toThrowMatchingStringOrRegexp(matcherName, error, expected, value);
  53. } else if (expected && typeof expected === 'object') {
  54. return toThrowMatchingErrorInstance(matcherName, error, expected);
  55. } else if (expected === undefined) {
  56. const pass = error !== undefined;
  57. return {
  58. message: pass
  59. ? () =>
  60. (0, _jestMatcherUtils.matcherHint)(
  61. '.not' + matcherName,
  62. 'function',
  63. ''
  64. ) +
  65. '\n\n' +
  66. 'Expected the function not to throw an error.\n' +
  67. printActualErrorMessage(error)
  68. : () =>
  69. (0, _jestMatcherUtils.matcherHint)(
  70. matcherName,
  71. 'function',
  72. (0, _jestGetType2.default)(value)
  73. ) +
  74. '\n\n' +
  75. 'Expected the function to throw an error.\n' +
  76. printActualErrorMessage(error),
  77. pass: pass
  78. };
  79. } else {
  80. throw new Error(
  81. (0, _jestMatcherUtils.matcherHint)(
  82. '.not' + matcherName,
  83. 'function',
  84. (0, _jestGetType2.default)(value)
  85. ) +
  86. '\n\n' +
  87. 'Unexpected argument passed.\nExpected: ' +
  88. `${(0, _jestMatcherUtils.printExpected)('string')}, ${(0,
  89. _jestMatcherUtils.printExpected)('Error (type)')} or ${(0,
  90. _jestMatcherUtils.printExpected)('regexp')}.\n` +
  91. (0, _jestMatcherUtils.printWithType)(
  92. 'Got',
  93. String(expected),
  94. _jestMatcherUtils.printExpected
  95. )
  96. );
  97. }
  98. });
  99. /**
  100. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  101. *
  102. * This source code is licensed under the MIT license found in the
  103. * LICENSE file in the root directory of this source tree.
  104. *
  105. *
  106. */
  107. const matchers = {
  108. toThrow: createMatcher('.toThrow'),
  109. toThrowError: createMatcher('.toThrowError')
  110. };
  111. const toThrowMatchingStringOrRegexp = (name, error, pattern, value) => {
  112. if (error && !error.message && !error.name) {
  113. error = new Error(error);
  114. }
  115. const pass = !!(error && error.message.match(pattern));
  116. const message = pass
  117. ? () =>
  118. (0, _jestMatcherUtils.matcherHint)(
  119. '.not' + name,
  120. 'function',
  121. (0, _jestGetType2.default)(value)
  122. ) +
  123. '\n\n' +
  124. `Expected the function not to throw an error matching:\n` +
  125. ` ${(0, _jestMatcherUtils.printExpected)(value)}\n` +
  126. printActualErrorMessage(error)
  127. : () =>
  128. (0, _jestMatcherUtils.matcherHint)(
  129. name,
  130. 'function',
  131. (0, _jestGetType2.default)(value)
  132. ) +
  133. '\n\n' +
  134. `Expected the function to throw an error matching:\n` +
  135. ` ${(0, _jestMatcherUtils.printExpected)(value)}\n` +
  136. printActualErrorMessage(error);
  137. return {message: message, pass: pass};
  138. };
  139. const toThrowMatchingErrorInstance = (name, error, expectedError) => {
  140. if (error && !error.message && !error.name) {
  141. error = new Error(error);
  142. }
  143. const pass = (0, _jasmine_utils.equals)(error, expectedError);
  144. const message = pass
  145. ? () =>
  146. (0, _jestMatcherUtils.matcherHint)('.not' + name, 'function', 'error') +
  147. '\n\n' +
  148. `Expected the function not to throw an error matching:\n` +
  149. ` ${(0, _jestMatcherUtils.printExpected)(expectedError)}\n` +
  150. printActualErrorMessage(error)
  151. : () =>
  152. (0, _jestMatcherUtils.matcherHint)(name, 'function', 'error') +
  153. '\n\n' +
  154. `Expected the function to throw an error matching:\n` +
  155. ` ${(0, _jestMatcherUtils.printExpected)(expectedError)}\n` +
  156. printActualErrorMessage(error);
  157. return {message: message, pass: pass};
  158. };
  159. const toThrowMatchingError = (name, error, ErrorClass) => {
  160. const pass = !!(error && error instanceof ErrorClass);
  161. const message = pass
  162. ? () =>
  163. (0, _jestMatcherUtils.matcherHint)('.not' + name, 'function', 'type') +
  164. '\n\n' +
  165. `Expected the function not to throw an error of type:\n` +
  166. ` ${(0, _jestMatcherUtils.printExpected)(ErrorClass.name)}\n` +
  167. printActualErrorMessage(error)
  168. : () =>
  169. (0, _jestMatcherUtils.matcherHint)(name, 'function', 'type') +
  170. '\n\n' +
  171. `Expected the function to throw an error of type:\n` +
  172. ` ${(0, _jestMatcherUtils.printExpected)(ErrorClass.name)}\n` +
  173. printActualErrorMessage(error);
  174. return {message: message, pass: pass};
  175. };
  176. const printActualErrorMessage = error => {
  177. if (error) {
  178. var _separateMessageFromS = (0, _jestMessageUtil.separateMessageFromStack)(
  179. error.stack
  180. );
  181. const message = _separateMessageFromS.message,
  182. stack = _separateMessageFromS.stack;
  183. return (
  184. `Instead, it threw:\n` +
  185. (0, _jestMatcherUtils.RECEIVED_COLOR)(
  186. ' ' +
  187. (0, _jestMatcherUtils.highlightTrailingWhitespace)(message) +
  188. (0, _jestMessageUtil.formatStackTrace)(
  189. stack,
  190. {
  191. rootDir: process.cwd(),
  192. testMatch: []
  193. },
  194. {
  195. noStackTrace: false
  196. }
  197. )
  198. )
  199. );
  200. }
  201. return `But it didn't throw anything.`;
  202. };
  203. exports.default = matchers;