Skip to content

Commit 8ec4fa1

Browse files
committed
Augmented assertRequest, so it can catch unhandled rejections
1 parent b6ff09d commit 8ec4fa1

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

test/functional/test-utils.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,37 @@ export function assertRequest(ports: number[],
1313

1414
ports.forEach(port => {
1515

16-
it("asserting port " + port, () => {
17-
if (args === 4) {
18-
return chakram[method](`http://127.0.0.1:${port}/${route}`).then(dataOrCallback as Function);
19-
} else if (args === 5) {
20-
return chakram[method](`http://127.0.0.1:${port}/${route}`, dataOrCallback as any).then(dataOrRequestOptionsOrCallback as Function);
21-
} else if (args === 6) {
22-
return chakram[method](`http://127.0.0.1:${port}/${route}`, dataOrCallback as any, dataOrRequestOptionsOrCallback as any).then(maybeCallback);
16+
it("asserting port " + port, async() => {
17+
let unhandledRejection: Error = undefined;
18+
const captureRejection = (e: Error) => { unhandledRejection = e; };
19+
process.on("unhandledRejection", captureRejection);
20+
21+
try {
22+
let r;
23+
if (args === 4) {
24+
r = await chakram[method](`http://127.0.0.1:${port}/${route}`).then(dataOrCallback as Function);
25+
}
26+
else if (args === 5) {
27+
r = await chakram[method](`http://127.0.0.1:${port}/${route}`, dataOrCallback as any).then(dataOrRequestOptionsOrCallback as Function);
28+
}
29+
else if (args === 6) {
30+
r = await chakram[method](`http://127.0.0.1:${port}/${route}`, dataOrCallback as any, dataOrRequestOptionsOrCallback as any).then(maybeCallback);
31+
}
32+
else {
33+
throw new Error("No assertion has been performed");
34+
}
35+
36+
if (unhandledRejection) {
37+
const e = new Error("There was an unhandled rejection while processing the request");
38+
e.stack += "\nCaused by: " + unhandledRejection.stack;
39+
throw e;
40+
}
41+
42+
return r;
43+
}
44+
finally {
45+
process.removeListener("unhandledRejection", captureRejection);
2346
}
24-
25-
throw new Error("No assertion has been performed");
2647
});
2748

2849
});

0 commit comments

Comments
 (0)