UNPKG

2.48 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = void 0;
7/**
8 * Copyright (c) Meta Platforms, Inc. and affiliates.
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
14class ReporterDispatcher {
15 _reporters;
16 constructor() {
17 this._reporters = [];
18 }
19 register(reporter) {
20 this._reporters.push(reporter);
21 }
22 unregister(reporterConstructor) {
23 this._reporters = this._reporters.filter(
24 reporter => !(reporter instanceof reporterConstructor)
25 );
26 }
27 async onTestFileResult(test, testResult, results) {
28 for (const reporter of this._reporters) {
29 if (reporter.onTestFileResult) {
30 await reporter.onTestFileResult(test, testResult, results);
31 } else if (reporter.onTestResult) {
32 await reporter.onTestResult(test, testResult, results);
33 }
34 }
35
36 // Release memory if unused later.
37 testResult.coverage = undefined;
38 testResult.console = undefined;
39 }
40 async onTestFileStart(test) {
41 for (const reporter of this._reporters) {
42 if (reporter.onTestFileStart) {
43 await reporter.onTestFileStart(test);
44 } else if (reporter.onTestStart) {
45 await reporter.onTestStart(test);
46 }
47 }
48 }
49 async onRunStart(results, options) {
50 for (const reporter of this._reporters) {
51 reporter.onRunStart && (await reporter.onRunStart(results, options));
52 }
53 }
54 async onTestCaseStart(test, testCaseStartInfo) {
55 for (const reporter of this._reporters) {
56 if (reporter.onTestCaseStart) {
57 await reporter.onTestCaseStart(test, testCaseStartInfo);
58 }
59 }
60 }
61 async onTestCaseResult(test, testCaseResult) {
62 for (const reporter of this._reporters) {
63 if (reporter.onTestCaseResult) {
64 await reporter.onTestCaseResult(test, testCaseResult);
65 }
66 }
67 }
68 async onRunComplete(testContexts, results) {
69 for (const reporter of this._reporters) {
70 if (reporter.onRunComplete) {
71 await reporter.onRunComplete(testContexts, results);
72 }
73 }
74 }
75
76 // Return a list of last errors for every reporter
77 getErrors() {
78 return this._reporters.reduce((list, reporter) => {
79 const error = reporter.getLastError && reporter.getLastError();
80 return error ? list.concat(error) : list;
81 }, []);
82 }
83 hasErrors() {
84 return this.getErrors().length !== 0;
85 }
86}
87exports.default = ReporterDispatcher;