Skip to content

Commit

Permalink
e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoishin committed Jan 14, 2025
1 parent 1519bbe commit c95c0fb
Show file tree
Hide file tree
Showing 17 changed files with 428 additions and 86 deletions.
104 changes: 104 additions & 0 deletions e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { mkdtemp, readFile, rm } from "node:fs/promises";
import { EOL, tmpdir } from "node:os";
import { join } from "node:path";
import type { Readable } from "node:stream";
import { afterEach } from "node:test";

import { cursorDown } from "ansi-escapes";
import spawn from "nano-spawn";
import { beforeEach, describe, expect, test } from "vitest";

const waitForLine = (stdout: Readable, matcher?: RegExp | string) => {
return new Promise<string>((resolve, reject) => {
const onData = (data: string) => {
if (matcher) {
if (typeof matcher === "string") {
if (!data.includes(matcher)) {
return;
}
} else {
if (!matcher.test(data)) {
return;
}
}
}
stdout.removeListener("data", onData);
stdout.removeListener("close", onClose);
resolve(data);
};
const onClose = () => {
stdout.removeListener("data", onData);
stdout.removeListener("close", onClose);
reject(new Error("stdout closed"));
};
stdout.addListener("data", onData);
stdout.addListener("close", onClose);
});
};

const runBin = async () => {
const childProcess = spawn(
join(import.meta.dirname, "node_modules/.bin/tsx"),
[join(import.meta.dirname, "src/bin.ts")],
);
void (async () => {
for await (const data of childProcess.stdout) {
console.log(data);
}
})();
const nodeChildProcess = await childProcess.nodeChildProcess;
if (
!nodeChildProcess.stdin ||
!nodeChildProcess.stdout ||
!nodeChildProcess.stderr
) {
throw new Error("stdio is null");
}
return {
stdin: nodeChildProcess.stdin,
stdout: nodeChildProcess.stdout,
stderr: nodeChildProcess.stderr,
waitForExit: async () => {
await childProcess;
},
};
};

describe("CLI E2E Test", { timeout: 60_000 }, () => {
let dir: string | null = null;

beforeEach(async () => {
dir = await mkdtemp(tmpdir() + "/");
process.chdir(dir);
});

afterEach(async () => {
if (dir) {
await rm(dir, { recursive: true, force: true }).catch();
}
});

test("Vanilla", async () => {
if (!dir) {
throw new Error("dir is null");
}
const { stdout, stdin, waitForExit } = await runBin();
await waitForLine(stdout, "Select a template");
stdin.write(EOL);
await waitForLine(stdout, "The new project name");
stdin.write("project" + EOL);
await waitForLine(stdout, "The new bundle name");
stdin.write(EOL);
await waitForLine(stdout, "Initialize git repo?");
stdin.write(EOL);
await waitForLine(stdout, "Select package manager");
stdin.write(cursorDown() + EOL);
await waitForExit();

const packageJson = JSON.parse(
await readFile(join(dir, "project/package.json"), "utf-8"),
) as object;
expect(packageJson).toMatchObject({ name: "project" });
expect(Object.keys(packageJson)).toContain("nodecg");
});
});
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@
"@types/eslint-config-prettier": "^6.11.3",
"@types/node": "^22.10.5",
"@types/validate-npm-package-name": "^4.0.2",
"ansi-escapes": "^7.0.0",
"eslint": "^9.18.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-simple-import-sort": "^12.1.1",
"execa": "^9.5.2",
"prettier": "^3.4.2",
"tsup": "^8.3.5",
"tsx": "^4.19.2",
"typescript": "~5.7.3",
"typescript-eslint": "^8.19.1",
"vitest": "^2.1.8"
Expand Down
Loading

0 comments on commit c95c0fb

Please sign in to comment.