123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- 'use strict';
- Object.defineProperty(exports, '__esModule', {
- value: true
- });
- exports.createMatcher = undefined;
- var _jestGetType = require('jest-get-type');
- var _jestGetType2 = _interopRequireDefault(_jestGetType);
- var _jestRegexUtil = require('jest-regex-util');
- var _jestMessageUtil = require('jest-message-util');
- var _jestMatcherUtils = require('jest-matcher-utils');
- var _jasmine_utils = require('./jasmine_utils');
- var _utils = require('./utils');
- function _interopRequireDefault(obj) {
- return obj && obj.__esModule ? obj : {default: obj};
- }
- const createMatcher = (exports.createMatcher = (matcherName, fromPromise) => (
- actual,
- expected
- ) => {
- const value = expected;
- let error;
- if (fromPromise && (0, _utils.isError)(actual)) {
- error = actual;
- } else {
- if (typeof actual !== 'function') {
- if (!fromPromise) {
- 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`
- );
- }
- } else {
- try {
- actual();
- } catch (e) {
- error = e;
- }
- }
- }
- if (typeof expected === 'string') {
- expected = new RegExp((0, _jestRegexUtil.escapeStrForRegex)(expected));
- }
- if (typeof expected === 'function') {
- return toThrowMatchingError(matcherName, error, expected);
- } else if (expected && typeof expected.test === 'function') {
- return toThrowMatchingStringOrRegexp(matcherName, error, expected, value);
- } else if (expected && typeof expected === 'object') {
- return toThrowMatchingErrorInstance(matcherName, error, expected);
- } else if (expected === undefined) {
- const pass = error !== undefined;
- return {
- 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),
- pass: pass
- };
- } else {
- 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
- )
- );
- }
- });
- /**
- * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
- *
- * This source code is licensed under the MIT license found in the
- * LICENSE file in the root directory of this source tree.
- *
- *
- */
- const matchers = {
- toThrow: createMatcher('.toThrow'),
- toThrowError: createMatcher('.toThrowError')
- };
- const toThrowMatchingStringOrRegexp = (name, error, pattern, value) => {
- if (error && !error.message && !error.name) {
- error = new Error(error);
- }
- const pass = !!(error && error.message.match(pattern));
- 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);
- return {message: message, pass: pass};
- };
- const toThrowMatchingErrorInstance = (name, error, expectedError) => {
- if (error && !error.message && !error.name) {
- error = new Error(error);
- }
- const pass = (0, _jasmine_utils.equals)(error, expectedError);
- 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);
- return {message: message, pass: pass};
- };
- const toThrowMatchingError = (name, error, ErrorClass) => {
- const pass = !!(error && error instanceof ErrorClass);
- 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);
- return {message: message, pass: pass};
- };
- const printActualErrorMessage = error => {
- if (error) {
- var _separateMessageFromS = (0, _jestMessageUtil.separateMessageFromStack)(
- error.stack
- );
- const message = _separateMessageFromS.message,
- stack = _separateMessageFromS.stack;
- return (
- `Instead, it threw:\n` +
- (0, _jestMatcherUtils.RECEIVED_COLOR)(
- ' ' +
- (0, _jestMatcherUtils.highlightTrailingWhitespace)(message) +
- (0, _jestMessageUtil.formatStackTrace)(
- stack,
- {
- rootDir: process.cwd(),
- testMatch: []
- },
- {
- noStackTrace: false
- }
- )
- )
- );
- }
- return `But it didn't throw anything.`;
- };
- exports.default = matchers;
|