extract_expected_assertions_errors.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 matcher's 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. expectedAssertionsNumberError = _getState.expectedAssertionsNumberError,
  30. isExpectingAssertions = _getState.isExpectingAssertions,
  31. isExpectingAssertionsError = _getState.isExpectingAssertionsError;
  32. resetAssertionsLocalState();
  33. if (
  34. typeof expectedAssertionsNumber === 'number' &&
  35. assertionCalls !== expectedAssertionsNumber
  36. ) {
  37. const numOfAssertionsExpected = (0, _jestMatcherUtils.EXPECTED_COLOR)(
  38. (0, _jestMatcherUtils.pluralize)('assertion', expectedAssertionsNumber)
  39. );
  40. expectedAssertionsNumberError.message =
  41. (0, _jestMatcherUtils.matcherHint)(
  42. '.assertions',
  43. '',
  44. String(expectedAssertionsNumber),
  45. {
  46. isDirectExpectCall: true
  47. }
  48. ) +
  49. '\n\n' +
  50. `Expected ${numOfAssertionsExpected} to be called but received ` +
  51. (0, _jestMatcherUtils.RECEIVED_COLOR)(
  52. (0, _jestMatcherUtils.pluralize)('assertion call', assertionCalls || 0)
  53. ) +
  54. '.';
  55. result.push({
  56. actual: assertionCalls,
  57. error: expectedAssertionsNumberError,
  58. expected: expectedAssertionsNumber
  59. });
  60. }
  61. if (isExpectingAssertions && assertionCalls === 0) {
  62. const expected = (0, _jestMatcherUtils.EXPECTED_COLOR)(
  63. 'at least one assertion'
  64. );
  65. const received = (0, _jestMatcherUtils.RECEIVED_COLOR)('received none');
  66. isExpectingAssertionsError.message =
  67. (0, _jestMatcherUtils.matcherHint)('.hasAssertions', '', '', {
  68. isDirectExpectCall: true
  69. }) +
  70. '\n\n' +
  71. `Expected ${expected} to be called but ${received}.`;
  72. result.push({
  73. actual: 'none',
  74. error: isExpectingAssertionsError,
  75. expected: 'at least one'
  76. });
  77. }
  78. return result;
  79. };
  80. exports.default = extractExpectedAssertionsErrors;