extractExpectedAssertionsErrors.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _jestMatcherUtils = require('jest-matcher-utils');
  7. var _jestMatchersObject = require('./jestMatchersObject');
  8. /**
  9. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  10. *
  11. * This source code is licensed under the MIT license found in the
  12. * LICENSE file in the root directory of this source tree.
  13. *
  14. */
  15. const resetAssertionsLocalState = () => {
  16. (0, _jestMatchersObject.setState)({
  17. assertionCalls: 0,
  18. expectedAssertionsNumber: null,
  19. isExpectingAssertions: false
  20. });
  21. }; // Create and format all errors related to the mismatched number of `expect`
  22. // calls and reset the matcher's state.
  23. const extractExpectedAssertionsErrors = () => {
  24. const result = [];
  25. const _getState = (0, _jestMatchersObject.getState)(),
  26. assertionCalls = _getState.assertionCalls,
  27. expectedAssertionsNumber = _getState.expectedAssertionsNumber,
  28. expectedAssertionsNumberError = _getState.expectedAssertionsNumberError,
  29. isExpectingAssertions = _getState.isExpectingAssertions,
  30. isExpectingAssertionsError = _getState.isExpectingAssertionsError;
  31. resetAssertionsLocalState();
  32. if (
  33. typeof expectedAssertionsNumber === 'number' &&
  34. assertionCalls !== expectedAssertionsNumber
  35. ) {
  36. const numOfAssertionsExpected = (0, _jestMatcherUtils.EXPECTED_COLOR)(
  37. (0, _jestMatcherUtils.pluralize)('assertion', expectedAssertionsNumber)
  38. );
  39. expectedAssertionsNumberError.message =
  40. (0, _jestMatcherUtils.matcherHint)(
  41. '.assertions',
  42. '',
  43. String(expectedAssertionsNumber),
  44. {
  45. isDirectExpectCall: true
  46. }
  47. ) +
  48. '\n\n' +
  49. `Expected ${numOfAssertionsExpected} to be called but received ` +
  50. (0, _jestMatcherUtils.RECEIVED_COLOR)(
  51. (0, _jestMatcherUtils.pluralize)('assertion call', assertionCalls || 0)
  52. ) +
  53. '.';
  54. result.push({
  55. actual: assertionCalls,
  56. error: expectedAssertionsNumberError,
  57. expected: expectedAssertionsNumber
  58. });
  59. }
  60. if (isExpectingAssertions && assertionCalls === 0) {
  61. const expected = (0, _jestMatcherUtils.EXPECTED_COLOR)(
  62. 'at least one assertion'
  63. );
  64. const received = (0, _jestMatcherUtils.RECEIVED_COLOR)('received none');
  65. isExpectingAssertionsError.message =
  66. (0, _jestMatcherUtils.matcherHint)('.hasAssertions', '', '', {
  67. isDirectExpectCall: true
  68. }) +
  69. '\n\n' +
  70. `Expected ${expected} to be called but ${received}.`;
  71. result.push({
  72. actual: 'none',
  73. error: isExpectingAssertionsError,
  74. expected: 'at least one'
  75. });
  76. }
  77. return result;
  78. };
  79. var _default = extractExpectedAssertionsErrors;
  80. exports.default = _default;