index.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. 'use strict';
  2. var _jestMatcherUtils = require('jest-matcher-utils');
  3. var utils = _interopRequireWildcard(_jestMatcherUtils);
  4. var _matchers = require('./matchers');
  5. var _matchers2 = _interopRequireDefault(_matchers);
  6. var _spy_matchers = require('./spy_matchers');
  7. var _spy_matchers2 = _interopRequireDefault(_spy_matchers);
  8. var _to_throw_matchers = require('./to_throw_matchers');
  9. var _to_throw_matchers2 = _interopRequireDefault(_to_throw_matchers);
  10. var _jasmine_utils = require('./jasmine_utils');
  11. var _asymmetric_matchers = require('./asymmetric_matchers');
  12. var _jest_matchers_object = require('./jest_matchers_object');
  13. var _extract_expected_assertions_errors = require('./extract_expected_assertions_errors');
  14. var _extract_expected_assertions_errors2 = _interopRequireDefault(_extract_expected_assertions_errors);
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  17. class JestAssertionError extends Error {} /**
  18. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  19. *
  20. * This source code is licensed under the MIT license found in the
  21. * LICENSE file in the root directory of this source tree.
  22. *
  23. *
  24. */
  25. const isPromise = obj => {
  26. return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
  27. };
  28. const createToThrowErrorMatchingSnapshotMatcher = function (matcher) {
  29. return function (received, testName) {
  30. return matcher.apply(this, [received, testName, true]);
  31. };
  32. };
  33. const getPromiseMatcher = (name, matcher) => {
  34. if (name === 'toThrow' || name === 'toThrowError') {
  35. return (0, _to_throw_matchers.createMatcher)('.' + name, true);
  36. } else if (name === 'toThrowErrorMatchingSnapshot') {
  37. return createToThrowErrorMatchingSnapshotMatcher(matcher);
  38. }
  39. return null;
  40. };
  41. const expect = function (actual) {
  42. if ((arguments.length <= 1 ? 0 : arguments.length - 1) !== 0) {
  43. throw new Error('Expect takes at most one argument.');
  44. }
  45. const allMatchers = (0, _jest_matchers_object.getMatchers)();
  46. const expectation = {
  47. not: {},
  48. rejects: { not: {} },
  49. resolves: { not: {} }
  50. };
  51. Object.keys(allMatchers).forEach(name => {
  52. const matcher = allMatchers[name];
  53. const promiseMatcher = getPromiseMatcher(name, matcher) || matcher;
  54. expectation[name] = makeThrowingMatcher(matcher, false, actual);
  55. expectation.not[name] = makeThrowingMatcher(matcher, true, actual);
  56. expectation.resolves[name] = makeResolveMatcher(name, promiseMatcher, false, actual);
  57. expectation.resolves.not[name] = makeResolveMatcher(name, promiseMatcher, true, actual);
  58. expectation.rejects[name] = makeRejectMatcher(name, promiseMatcher, false, actual);
  59. expectation.rejects.not[name] = makeRejectMatcher(name, matcher, true, actual);
  60. });
  61. return expectation;
  62. };
  63. const getMessage = message => {
  64. return message && message() || utils.RECEIVED_COLOR('No message was specified for this matcher.');
  65. };
  66. const makeResolveMatcher = (matcherName, matcher, isNot, actual) => function () {
  67. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  68. args[_key] = arguments[_key];
  69. }
  70. const matcherStatement = `.resolves.${isNot ? 'not.' : ''}${matcherName}`;
  71. if (!isPromise(actual)) {
  72. throw new JestAssertionError(utils.matcherHint(matcherStatement, 'received', '') + '\n\n' + `${utils.RECEIVED_COLOR('received')} value must be a Promise.\n` + utils.printWithType('Received', actual, utils.printReceived));
  73. }
  74. return actual.then(result => makeThrowingMatcher(matcher, isNot, result).apply(null, args), reason => {
  75. const err = new JestAssertionError(utils.matcherHint(matcherStatement, 'received', '') + '\n\n' + `Expected ${utils.RECEIVED_COLOR('received')} Promise to resolve, ` + 'instead it rejected to value\n' + ` ${utils.printReceived(reason)}`);
  76. return Promise.reject(err);
  77. });
  78. };
  79. const makeRejectMatcher = (matcherName, matcher, isNot, actual) => function () {
  80. for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  81. args[_key2] = arguments[_key2];
  82. }
  83. const matcherStatement = `.rejects.${isNot ? 'not.' : ''}${matcherName}`;
  84. if (!isPromise(actual)) {
  85. throw new JestAssertionError(utils.matcherHint(matcherStatement, 'received', '') + '\n\n' + `${utils.RECEIVED_COLOR('received')} value must be a Promise.\n` + utils.printWithType('Received', actual, utils.printReceived));
  86. }
  87. return actual.then(result => {
  88. const err = new JestAssertionError(utils.matcherHint(matcherStatement, 'received', '') + '\n\n' + `Expected ${utils.RECEIVED_COLOR('received')} Promise to reject, ` + 'instead it resolved to value\n' + ` ${utils.printReceived(result)}`);
  89. return Promise.reject(err);
  90. }, reason => makeThrowingMatcher(matcher, isNot, reason).apply(null, args));
  91. };
  92. const makeThrowingMatcher = (matcher, isNot, actual) => {
  93. return function throwingMatcher() {
  94. let throws = true;
  95. const matcherContext = Object.assign(
  96. // When throws is disabled, the matcher will not throw errors during test
  97. // execution but instead add them to the global matcher state. If a
  98. // matcher throws, test execution is normally stopped immediately. The
  99. // snapshot matcher uses it because we want to log all snapshot
  100. // failures in a test.
  101. { dontThrow: () => throws = false }, (0, _jest_matchers_object.getState)(), {
  102. equals: _jasmine_utils.equals,
  103. isNot,
  104. utils
  105. });
  106. let result;
  107. try {
  108. for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
  109. args[_key3] = arguments[_key3];
  110. }
  111. result = matcher.apply(matcherContext, [actual].concat(args));
  112. } catch (error) {
  113. if (matcher[_jest_matchers_object.INTERNAL_MATCHER_FLAG] === true && !(error instanceof JestAssertionError) && error.name !== 'PrettyFormatPluginError' &&
  114. // Guard for some environments (browsers) that do not support this feature.
  115. Error.captureStackTrace) {
  116. // Try to remove this and deeper functions from the stack trace frame.
  117. Error.captureStackTrace(error, throwingMatcher);
  118. }
  119. throw error;
  120. }
  121. _validateResult(result);
  122. (0, _jest_matchers_object.getState)().assertionCalls++;
  123. if (result.pass && isNot || !result.pass && !isNot) {
  124. // XOR
  125. const message = getMessage(result.message);
  126. const error = new JestAssertionError(message);
  127. // Passing the result of the matcher with the error so that a custom
  128. // reporter could access the actual and expected objects of the result
  129. // for example in order to display a custom visual diff
  130. error.matcherResult = result;
  131. // Try to remove this function from the stack trace frame.
  132. // Guard for some environments (browsers) that do not support this feature.
  133. if (Error.captureStackTrace) {
  134. Error.captureStackTrace(error, throwingMatcher);
  135. }
  136. if (throws) {
  137. throw error;
  138. } else {
  139. (0, _jest_matchers_object.getState)().suppressedErrors.push(error);
  140. }
  141. }
  142. };
  143. };
  144. expect.extend = matchers => (0, _jest_matchers_object.setMatchers)(matchers, false);
  145. expect.anything = _asymmetric_matchers.anything;
  146. expect.any = _asymmetric_matchers.any;
  147. expect.objectContaining = _asymmetric_matchers.objectContaining;
  148. expect.arrayContaining = _asymmetric_matchers.arrayContaining;
  149. expect.stringContaining = _asymmetric_matchers.stringContaining;
  150. expect.stringMatching = _asymmetric_matchers.stringMatching;
  151. const _validateResult = result => {
  152. if (typeof result !== 'object' || typeof result.pass !== 'boolean' || result.message && typeof result.message !== 'string' && typeof result.message !== 'function') {
  153. throw new Error('Unexpected return from a matcher function.\n' + 'Matcher functions should ' + 'return an object in the following format:\n' + ' {message?: string | function, pass: boolean}\n' + `'${utils.stringify(result)}' was returned`);
  154. }
  155. };
  156. // add default jest matchers
  157. (0, _jest_matchers_object.setMatchers)(_matchers2.default, true);
  158. (0, _jest_matchers_object.setMatchers)(_spy_matchers2.default, true);
  159. (0, _jest_matchers_object.setMatchers)(_to_throw_matchers2.default, true);
  160. expect.addSnapshotSerializer = () => void 0;
  161. expect.assertions = expected => {
  162. (0, _jest_matchers_object.getState)().expectedAssertionsNumber = expected;
  163. };
  164. expect.hasAssertions = expected => {
  165. utils.ensureNoExpected(expected, '.hasAssertions');
  166. (0, _jest_matchers_object.getState)().isExpectingAssertions = true;
  167. };
  168. expect.getState = _jest_matchers_object.getState;
  169. expect.setState = _jest_matchers_object.setState;
  170. expect.extractExpectedAssertionsErrors = _extract_expected_assertions_errors2.default;
  171. module.exports = expect;