From 377de8c085da0624473f3917dbc86e236ee692dd Mon Sep 17 00:00:00 2001 From: Marcin Gordel Date: Fri, 6 Sep 2024 13:46:13 +0200 Subject: [PATCH] test: added extra handlers for spawned tests --- tests/examples/examples.test.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/examples/examples.test.ts b/tests/examples/examples.test.ts index 539c3c95e..72182cd84 100644 --- a/tests/examples/examples.test.ts +++ b/tests/examples/examples.test.ts @@ -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);