Skip to content

Commit 4846ecc

Browse files
committed
fix: await reporter lifecycle events if neeeded
1 parent 01a6a75 commit 4846ecc

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

src/coverage.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,15 @@ export default async (options: IOptions) => {
7575
rules = options.rules;
7676
}
7777

78-
for (const reporter of options.reporters) {
79-
reporter.onBegin(options, calls);
80-
}
78+
await Promise.all(
79+
options.reporters.map((reporter) =>
80+
Promise.resolve(reporter.onBegin(options, calls))
81+
)
82+
);
8183

82-
for (const rule of rules) {
83-
await Promise.resolve(rule.onBegin?.(options));
84-
}
84+
await Promise.all(
85+
rules.map((rule) => Promise.resolve(rule.onBegin?.(options)))
86+
);
8587

8688
// getCalls could be async or sync
8789
const callsPromises = await Promise.all(filteredMethods.map((method) =>
@@ -109,9 +111,11 @@ export default async (options: IOptions) => {
109111
getCallsEnd: Date.now(),
110112
}
111113
}
112-
for (const reporter of options.reporters) {
113-
reporter.onTestBegin(options, call);
114-
}
114+
await Promise.all(
115+
options.reporters.map((reporter) =>
116+
Promise.resolve(reporter.onTestBegin(options, call))
117+
)
118+
);
115119
if (call.timings) {
116120
call.timings.beforeRequestStart = Date.now();
117121
}
@@ -161,17 +165,21 @@ export default async (options: IOptions) => {
161165
call.timings.afterResponseEnd = Date.now();
162166
call.timings.endTime = Date.now();
163167
}
164-
for (const reporter of options.reporters) {
165-
reporter.onTestEnd(options, call);
166-
}
168+
await Promise.all(
169+
options.reporters.map((reporter) =>
170+
Promise.resolve(reporter.onTestEnd(options, call))
171+
)
172+
);
167173
}
168174

169-
for (const rule of rules) {
170-
await Promise.resolve(rule.onEnd?.(options, calls));
171-
}
175+
await Promise.all(
176+
rules.map((rule) => Promise.resolve(rule.onEnd?.(options, calls)))
177+
);
172178

173-
for (const reporter of options.reporters) {
174-
reporter.onEnd(options, calls);
175-
}
179+
await Promise.all(
180+
options.reporters.map((reporter) =>
181+
Promise.resolve(reporter.onEnd(options, calls))
182+
)
183+
);
176184
return calls;
177185
};

src/reporters/reporter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Call, IOptions } from "../coverage";
22

33
interface Reporter {
4-
onBegin(options: IOptions, calls: Call[]): void;
5-
onTestBegin(options: IOptions, call: Call): void;
6-
onTestEnd(options: IOptions, call: Call): void;
7-
onEnd(options: IOptions, calls: Call[]): void;
4+
onBegin(options: IOptions, calls: Call[]): void | Promise<void>;
5+
onTestBegin(options: IOptions, call: Call): void | Promise<void>;
6+
onTestEnd(options: IOptions, call: Call): void | Promise<void>;
7+
onEnd(options: IOptions, calls: Call[]): void | Promise<void>;
88
}
99

1010
export default Reporter;

0 commit comments

Comments
 (0)