extract_expected_assertions_errors.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _jestMatcherUtils = require('jest-matcher-utils');
  6. var _jest_matchers_object = require('./jest_matchers_object');
  7. /**
  8. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  9. *
  10. * This source code is licensed under the MIT license found in the
  11. * LICENSE file in the root directory of this source tree.
  12. *
  13. *
  14. */
  15. const resetAssertionsLocalState = () => {
  16. (0, _jest_matchers_object.setState)({
  17. assertionCalls: 0,
  18. expectedAssertionsNumber: null,
  19. isExpectingAssertions: false
  20. });
  21. };
  22. // Create and format all errors related to the mismatched number of `expect`
  23. // calls and reset the matchers state.
  24. const extractExpectedAssertionsErrors = () => {
  25. const result = [];
  26. var _getState = (0, _jest_matchers_object.getState)();
  27. const assertionCalls = _getState.assertionCalls,
  28. expectedAssertionsNumber = _getState.expectedAssertionsNumber,
  29. isExpectingAssertions = _getState.isExpectingAssertions;
  30. resetAssertionsLocalState();
  31. if (typeof expectedAssertionsNumber === 'number' && assertionCalls !== expectedAssertionsNumber) {
  32. const numOfAssertionsExpected = (0, _jestMatcherUtils.EXPECTED_COLOR)((0, _jestMatcherUtils.pluralize)('assertion', expectedAssertionsNumber));
  33. const error = new Error((0, _jestMatcherUtils.matcherHint)('.assertions', '', String(expectedAssertionsNumber), {
  34. isDirectExpectCall: true
  35. }) + '\n\n' + `Expected ${numOfAssertionsExpected} to be called but received ` + (0, _jestMatcherUtils.RECEIVED_COLOR)((0, _jestMatcherUtils.pluralize)('assertion call', assertionCalls || 0)) + '.');
  36. result.push({
  37. actual: assertionCalls,
  38. error,
  39. expected: expectedAssertionsNumber
  40. });
  41. }
  42. if (isExpectingAssertions && assertionCalls === 0) {
  43. const expected = (0, _jestMatcherUtils.EXPECTED_COLOR)('at least one assertion');
  44. const received = (0, _jestMatcherUtils.RECEIVED_COLOR)('received none');
  45. const error = new Error((0, _jestMatcherUtils.matcherHint)('.hasAssertions', '', '', {
  46. isDirectExpectCall: true
  47. }) + '\n\n' + `Expected ${expected} to be called but ${received}.`);
  48. result.push({
  49. actual: 'none',
  50. error,
  51. expected: 'at least one'
  52. });
  53. }
  54. return result;
  55. };
  56. exports.default = extractExpectedAssertionsErrors;