Skip to content

Commit

Permalink
test: added extra handlers for spawned tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mgordel committed Sep 6, 2024
1 parent 1e08e87 commit 377de8c
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion tests/examples/examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,22 @@ async function test(cmd: string, path: string, args: string[] = [], timeout = 36
};
spawnedExample.stdout?.on("data", assertLogs);
spawnedExample.stderr?.on("data", assertLogs);
spawnedExample.on("exit", (code, signal) => {
let isFinishing = false;
const finishTest = (code?: number, signal?: string) => {
if (isFinishing) {
console.log("Test finishing has already been triggered by another event");
return;
}
isFinishing = true;
clearTimeout(timeoutId);
if (!error && !code) return res(true);
rej(`Test example "${file}" failed. Exited with code ${code} by signal ${signal}. ${error}`);
};
spawnedExample.on("close", finishTest);
spawnedExample.on("exit", finishTest);
spawnedExample.on("error", (err) => {
error = `The test ended with an error: ${err}`;
spawnedExample.kill();
});
}).finally(() => {
clearTimeout(timeoutId);
Expand Down

0 comments on commit 377de8c

Please sign in to comment.