UNPKG

2.61 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 */
28
29/* eslint-disable local/ban-types-eventually */
30class ReporterDispatcher {
31 constructor() {
32 _defineProperty(this, '_reporters', void 0);
33
34 this._reporters = [];
35 }
36
37 register(reporter) {
38 this._reporters.push(reporter);
39 }
40
41 unregister(ReporterClass) {
42 this._reporters = this._reporters.filter(
43 reporter => !(reporter instanceof ReporterClass)
44 );
45 }
46
47 async onTestFileResult(test, testResult, results) {
48 for (const reporter of this._reporters) {
49 if (reporter.onTestFileResult) {
50 await reporter.onTestFileResult(test, testResult, results);
51 } else if (reporter.onTestResult) {
52 await reporter.onTestResult(test, testResult, results);
53 }
54 } // Release memory if unused later.
55
56 testResult.coverage = undefined;
57 testResult.console = undefined;
58 }
59
60 async onTestFileStart(test) {
61 for (const reporter of this._reporters) {
62 if (reporter.onTestFileStart) {
63 await reporter.onTestFileStart(test);
64 } else if (reporter.onTestStart) {
65 await reporter.onTestStart(test);
66 }
67 }
68 }
69
70 async onRunStart(results, options) {
71 for (const reporter of this._reporters) {
72 reporter.onRunStart && (await reporter.onRunStart(results, options));
73 }
74 }
75
76 async onTestCaseResult(test, testCaseResult) {
77 for (const reporter of this._reporters) {
78 if (reporter.onTestCaseResult) {
79 await reporter.onTestCaseResult(test, testCaseResult);
80 }
81 }
82 }
83
84 async onRunComplete(contexts, results) {
85 for (const reporter of this._reporters) {
86 if (reporter.onRunComplete) {
87 await reporter.onRunComplete(contexts, results);
88 }
89 }
90 } // Return a list of last errors for every reporter
91
92 getErrors() {
93 return this._reporters.reduce((list, reporter) => {
94 const error = reporter.getLastError && reporter.getLastError();
95 return error ? list.concat(error) : list;
96 }, []);
97 }
98
99 hasErrors() {
100 return this.getErrors().length !== 0;
101 }
102}
103
104exports.default = ReporterDispatcher;