From d850b47d405aadc7ed4b21fd336bf03bf970eb00 Mon Sep 17 00:00:00 2001 From: Marcin Gordel Date: Wed, 20 Sep 2023 16:02:20 +0200 Subject: [PATCH] test(examples): fixed examples --- tests/examples/examples.json | 2 +- tests/examples/examples.test.ts | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/tests/examples/examples.json b/tests/examples/examples.json index 62ff8f691..9bcd3d54d 100644 --- a/tests/examples/examples.json +++ b/tests/examples/examples.json @@ -27,7 +27,7 @@ { "cmd": "node", "path": "examples/docs-examples/examples/transferring-data/upload-file.mjs" }, { "cmd": "node", "path": "examples/docs-examples/examples/transferring-data/upload-json.mjs" }, - { "cmd": "node", "path": "examples/docs-examples/examples/using-app-key/index.mjs" }, + { "cmd": "node", "path": "examples/docs-examples/examples/using-app-keys/index.mjs" }, { "cmd": "node", "path": "examples/docs-examples/examples/working-with-images/hash.mjs" }, { "cmd": "node", "path": "examples/docs-examples/examples/working-with-images/tag.mjs" }, diff --git a/tests/examples/examples.test.ts b/tests/examples/examples.test.ts index 168fd82ed..783da6968 100644 --- a/tests/examples/examples.test.ts +++ b/tests/examples/examples.test.ts @@ -25,24 +25,28 @@ async function test(cmd: string, path: string, args: string[] = [], timeout = 18 const cwd = dirname(path); const spawnedExample = spawn(cmd, [file, ...args], { cwd }); spawnedExample.stdout?.setEncoding("utf-8"); - const timeoutId = setTimeout(() => spawnedExample.kill("SIGINT"), timeout * 1000); + let error = ""; + const timeoutId = setTimeout(() => { + error = `Test timeout was reached after ${timeout} seconds.`; + spawnedExample.kill("SIGTERM"); + spawnedExample.kill("SIGKILL"); + }, timeout * 1000); return new Promise((res, rej) => { spawnedExample.stdout?.on("data", (data: string) => { console.log(data.trim()); if (criticalLogsRegExp.some((regexp) => data.match(regexp))) { + error = `A critical error occurred during the test.`; spawnedExample.kill("SIGTERM"); + spawnedExample.kill("SIGKILL"); } }); - spawnedExample.on("close", (code, signal) => { - if (signal === null) return res(true); - let errorMsg = ""; - if (signal === "SIGINT") errorMsg = `Test timeout was reached after ${timeout} seconds.`; - if (signal === "SIGTERM") errorMsg = `A critical error occurred during the test.`; - rej(`Test example "${file}" failed. ${errorMsg}`); + spawnedExample.on("close", (code) => { + if (!error && code === 0) return res(true); + rej(`Test example "${file}" failed. ${error}`); }); }).finally(() => { clearTimeout(timeoutId); - spawnedExample.kill(); + spawnedExample.kill("SIGKILL"); }); }