UNPKG

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