Skip to content

Commit

Permalink
test(examples): added assertions for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mgordel committed Sep 19, 2023
1 parent 9a29755 commit 0931959
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions tests/examples/examples.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { spawn } from "child_process";
import { ChildProcess, spawn } from "child_process";
import { dirname, basename, resolve } from "path";
import { Goth } from "../goth/goth";

Expand All @@ -14,13 +14,13 @@ type Example = {

const examples: Example[] = [
{ cmd: "node", path: "examples/docs-examples/examples/composing-tasks/batch-end.mjs" },
{ cmd: "node", path: "docs-examples/examples/composing-tasks/batch-endstream-chunks.mjs" },
{ cmd: "node", path: "docs-examples/examples/composing-tasks/batch-endstream-forawait.mjs" },
{ cmd: "node", path: "docs-examples/examples/composing-tasks/multiple-run-prosaic.mjs" },
{ cmd: "node", path: "docs-examples/examples/composing-tasks/singleCommand.mjs" },
{ cmd: "node", path: "examples/docs-examples/examples/composing-tasks/batch-endstream-chunks.mjs" },
{ cmd: "node", path: "examples/docs-examples/examples/composing-tasks/batch-endstream-forawait.mjs" },
{ cmd: "node", path: "examples/docs-examples/examples/composing-tasks/multiple-run-prosaic.mjs" },
{ cmd: "node", path: "examples/docs-examples/examples/composing-tasks/singleCommand.mjs" },
];

const criticalLogsRegexp = [/Task timeot/, /Task rejected/, /TODO/];
const criticalLogsRegexp = [/Task timeot/, /Task *. has been rejected/, /ERROR: TypeError/];

const timeoutPromise = (seconds: number) =>
new Promise((res, rej) =>
Expand All @@ -29,12 +29,19 @@ const timeoutPromise = (seconds: number) =>
seconds * 1000,
),
);

const spawnedExamples: ChildProcess[] = [];
async function examplesTest(cmd: string, path: string, args: string[] = [], timeout = 120) {
const file = basename(path);
const cwd = dirname(path);
const spawnedExample = spawn(cmd, [file, ...args], { stdio: "inherit", cwd });
spawnedExamples.push(spawnedExample);
spawnedExample.stdout?.setEncoding("utf-8");
const testPromise = new Promise((res, rej) => {
spawnedExample.stdout?.on("data", (data: string) => {
if (criticalLogsRegexp.some((regexp) => data.match(regexp))) {
return rej(`Example test "${file}" failed.`);
}
});
spawnedExample.on("close", (code, signal) => {
if (code === 0) return res(true);
rej(`Example test "${file}" exited with code ${code} by signal ${signal}`);
Expand All @@ -53,6 +60,9 @@ async function testAll(examples: Example[]) {
} catch (error) {
console.error(error);
process.exit(1);
} finally {
await goth.end().catch((error) => console.error(error));
spawnedExamples.forEach((example) => example?.kill());
}
process.exit(0);
}
Expand Down

0 comments on commit 0931959

Please sign in to comment.