Skip to content

Commit

Permalink
test(examples): fixed examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mgordel committed Sep 20, 2023
1 parent 51a22d4 commit d850b47
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion tests/examples/examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
20 changes: 12 additions & 8 deletions tests/examples/examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
}

Expand Down

0 comments on commit d850b47

Please sign in to comment.