ReporterDispatcher.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _defineProperty(obj, key, value) {
  7. if (key in obj) {
  8. Object.defineProperty(obj, key, {
  9. value: value,
  10. enumerable: true,
  11. configurable: true,
  12. writable: true
  13. });
  14. } else {
  15. obj[key] = value;
  16. }
  17. return obj;
  18. }
  19. /**
  20. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  21. *
  22. * This source code is licensed under the MIT license found in the
  23. * LICENSE file in the root directory of this source tree.
  24. */
  25. /* eslint-disable local/ban-types-eventually */
  26. class ReporterDispatcher {
  27. constructor() {
  28. _defineProperty(this, '_reporters', void 0);
  29. this._reporters = [];
  30. }
  31. register(reporter) {
  32. this._reporters.push(reporter);
  33. }
  34. unregister(ReporterClass) {
  35. this._reporters = this._reporters.filter(
  36. reporter => !(reporter instanceof ReporterClass)
  37. );
  38. }
  39. async onTestFileResult(test, testResult, results) {
  40. for (const reporter of this._reporters) {
  41. if (reporter.onTestFileResult) {
  42. await reporter.onTestFileResult(test, testResult, results);
  43. } else if (reporter.onTestResult) {
  44. await reporter.onTestResult(test, testResult, results);
  45. }
  46. } // Release memory if unused later.
  47. testResult.sourceMaps = undefined;
  48. testResult.coverage = undefined;
  49. testResult.console = undefined;
  50. }
  51. async onTestFileStart(test) {
  52. for (const reporter of this._reporters) {
  53. if (reporter.onTestFileStart) {
  54. await reporter.onTestFileStart(test);
  55. } else if (reporter.onTestStart) {
  56. await reporter.onTestStart(test);
  57. }
  58. }
  59. }
  60. async onRunStart(results, options) {
  61. for (const reporter of this._reporters) {
  62. reporter.onRunStart && (await reporter.onRunStart(results, options));
  63. }
  64. }
  65. async onTestCaseResult(test, testCaseResult) {
  66. for (const reporter of this._reporters) {
  67. if (reporter.onTestCaseResult) {
  68. await reporter.onTestCaseResult(test, testCaseResult);
  69. }
  70. }
  71. }
  72. async onRunComplete(contexts, results) {
  73. for (const reporter of this._reporters) {
  74. if (reporter.onRunComplete) {
  75. await reporter.onRunComplete(contexts, results);
  76. }
  77. }
  78. } // Return a list of last errors for every reporter
  79. getErrors() {
  80. return this._reporters.reduce((list, reporter) => {
  81. const error = reporter.getLastError && reporter.getLastError();
  82. return error ? list.concat(error) : list;
  83. }, []);
  84. }
  85. hasErrors() {
  86. return this.getErrors().length !== 0;
  87. }
  88. }
  89. exports.default = ReporterDispatcher;