Skip to content

Commit ee334de

Browse files
committed
fix: refactor getExampleCalls to allow for it to be sync or async
1 parent 7fe84f8 commit ee334de

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/coverage.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,20 @@ export default async (options: IOptions) => {
5454
rules = options.rules;
5555
}
5656

57-
filteredMethods.forEach((method) => {
58-
rules.forEach((rule) =>
59-
rule.getExampleCalls(options.openrpcDocument, method)
60-
.forEach((exampleCall) => exampleCalls.push({...exampleCall, rule}))
61-
);
62-
});
57+
// getExampleCalls could be async or sync
58+
const exampleCallsPromises = await Promise.all(filteredMethods.map((method) =>
59+
Promise.all(
60+
rules.map(async (rule) => {
61+
const calls = await Promise.resolve(rule.getExampleCalls(options.openrpcDocument, method))
62+
calls.forEach((call) => {
63+
call.rule = rule;
64+
});
65+
return calls;
66+
}
67+
)
68+
)
69+
));
70+
exampleCalls.push(...exampleCallsPromises.flat().flat());
6371

6472
for (const reporter of options.reporters) {
6573
reporter.onBegin(options, exampleCalls);
@@ -86,7 +94,9 @@ export default async (options: IOptions) => {
8694
exampleCall.valid = false;
8795
exampleCall.requestError = e;
8896
}
89-
exampleCall.requestError ?? await Promise.resolve(exampleCall.rule?.validateExampleCall?.(exampleCall));
97+
if (exampleCall.requestError === undefined) {
98+
await Promise.resolve(exampleCall.rule?.validateExampleCall?.(exampleCall));
99+
}
90100
await Promise.resolve(exampleCall.rule?.afterResponse?.(options, exampleCall));
91101
for (const reporter of options.reporters) {
92102
reporter.onTestEnd(options, exampleCall);

src/rules/rule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ExampleCall, IOptions } from "../coverage";
33

44
interface Rule {
55
onBegin(options: IOptions, exampleCalls: ExampleCall[]): void;
6-
getExampleCalls(openrpcDocument: OpenrpcDocument, method: MethodObject): ExampleCall[];
6+
getExampleCalls(openrpcDocument: OpenrpcDocument, method: MethodObject): ExampleCall[] | Promise<ExampleCall[]>;
77
validateExampleCall(exampleCall: ExampleCall): Promise<ExampleCall> | ExampleCall;
88
onEnd(options: IOptions, exampleCalls: ExampleCall[]): void;
99
// example call lifecycle

0 commit comments

Comments
 (0)