ReporterDispatcher.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.coverage = undefined;
  48. testResult.console = undefined;
  49. }
  50. async onTestFileStart(test) {
  51. for (const reporter of this._reporters) {
  52. if (reporter.onTestFileStart) {
  53. await reporter.onTestFileStart(test);
  54. } else if (reporter.onTestStart) {
  55. await reporter.onTestStart(test);
  56. }
  57. }
  58. }
  59. async onRunStart(results, options) {
  60. for (const reporter of this._reporters) {
  61. reporter.onRunStart && (await reporter.onRunStart(results, options));
  62. }
  63. }
  64. async onTestCaseResult(test, testCaseResult) {
  65. for (const reporter of this._reporters) {
  66. if (reporter.onTestCaseResult) {
  67. await reporter.onTestCaseResult(test, testCaseResult);
  68. }
  69. }
  70. }
  71. async onRunComplete(contexts, results) {
  72. for (const reporter of this._reporters) {
  73. if (reporter.onRunComplete) {
  74. await reporter.onRunComplete(contexts, results);
  75. }
  76. }
  77. } // Return a list of last errors for every reporter
  78. getErrors() {
  79. return this._reporters.reduce((list, reporter) => {
  80. const error = reporter.getLastError && reporter.getLastError();
  81. return error ? list.concat(error) : list;
  82. }, []);
  83. }
  84. hasErrors() {
  85. return this.getErrors().length !== 0;
  86. }
  87. }
  88. exports.default = ReporterDispatcher;