diff --git a/test/cli/init/init.test.ts b/test/cli/init/init.test.ts index 9ee0efba90..b1923ba137 100644 --- a/test/cli/init/init.test.ts +++ b/test/cli/init/init.test.ts @@ -1,78 +1,104 @@ -import { expect, test } from "bun:test"; +import type { SyncSubprocess } from "bun"; +import { describe, beforeAll, afterAll, expect, test, it, jest } from "bun:test"; import fs from "fs"; import { bunEnv, bunExe, tmpdirSync } from "harness"; import path from "path"; -test("bun init works", () => { - const temp = tmpdirSync(); +jest.setTimeout(30_000); - const out = Bun.spawnSync({ - cmd: [bunExe(), "init", "-y"], - cwd: temp, - stdio: ["ignore", "inherit", "inherit"], - env: bunEnv, +/** package.json, README.md not included */ +const filesCreated = ["index.ts", ".gitignore", "node_modules", "tsconfig.json", "bunfig.toml"]; + +const defaultPackageJson = ({ name }) => ({ + "name": name, + "module": "index.ts", + "type": "module", + "devDependencies": { + "@types/bun": "latest", + }, + "peerDependencies": { + "typescript": "^5.0.0", + }, +}); + +describe("`bun init -y`", () => { + let temp: string; + let out: SyncSubprocess<"ignore", "inherit">; + + beforeAll(() => { + temp = tmpdirSync(); + out = Bun.spawnSync({ + cmd: [bunExe(), "init", "-y"], + cwd: temp, + stdio: ["ignore", "ignore", "inherit"], + env: bunEnv, + }); }); - expect(out.signal).toBe(undefined); - expect(out.exitCode).toBe(0); - - const pkg = JSON.parse(fs.readFileSync(path.join(temp, "package.json"), "utf8")); - expect(pkg).toEqual({ - "name": path.basename(temp).toLowerCase(), - "module": "index.ts", - "type": "module", - "devDependencies": { - "@types/bun": "latest", - }, - "peerDependencies": { - "typescript": "^5.0.0", - }, - }); - const readme = fs.readFileSync(path.join(temp, "README.md"), "utf8"); - expect(readme).toStartWith("# " + path.basename(temp).toLowerCase() + "\n"); - expect(readme).toInclude("v" + Bun.version.replaceAll("-debug", "")); - expect(readme).toInclude("index.ts"); - - expect(fs.existsSync(path.join(temp, "index.ts"))).toBe(true); - expect(fs.existsSync(path.join(temp, ".gitignore"))).toBe(true); - expect(fs.existsSync(path.join(temp, "node_modules"))).toBe(true); - expect(fs.existsSync(path.join(temp, "tsconfig.json"))).toBe(true); - expect(fs.existsSync(path.join(temp, "bunfig.toml"))).toBe(true); -}, 30_000); - -test("bun init with piped cli", () => { - const temp = tmpdirSync(); - - const out = Bun.spawnSync({ - cmd: [bunExe(), "init"], - cwd: temp, - stdio: [new Blob(["\n\n\n\n\n\n\n\n\n\n\n\n"]), "inherit", "inherit"], - env: bunEnv, + afterAll(() => { + Bun.$`rm -rf ${temp}`.nothrow(); }); - expect(out.signal).toBe(undefined); - expect(out.exitCode).toBe(0); - - const pkg = JSON.parse(fs.readFileSync(path.join(temp, "package.json"), "utf8")); - expect(pkg).toEqual({ - "name": path.basename(temp).toLowerCase(), - "module": "index.ts", - "type": "module", - "devDependencies": { - "@types/bun": "latest", - }, - "peerDependencies": { - "typescript": "^5.0.0", - }, + it("exits successfully", () => { + expect(out).not.toHaveProperty("signal"); + expect(out.exitCode).toBe(0); }); - const readme = fs.readFileSync(path.join(temp, "README.md"), "utf8"); - expect(readme).toStartWith("# " + path.basename(temp).toLowerCase() + "\n"); - expect(readme).toInclude("v" + Bun.version.replaceAll("-debug", "")); - expect(readme).toInclude("index.ts"); - expect(fs.existsSync(path.join(temp, "index.ts"))).toBe(true); - expect(fs.existsSync(path.join(temp, ".gitignore"))).toBe(true); - expect(fs.existsSync(path.join(temp, "node_modules"))).toBe(true); - expect(fs.existsSync(path.join(temp, "tsconfig.json"))).toBe(true); - expect(fs.existsSync(path.join(temp, "bunfig.toml"))).toBe(true); -}, 30_000); + it("creates the expected package.json", () => { + const pkg = JSON.parse(fs.readFileSync(path.join(temp, "package.json"), "utf8")); + const expected = defaultPackageJson({ name: path.basename(temp).toLowerCase() }); + expect(pkg).toEqual(expected); + }); + + it("populates the README.md template", () => { + const readme = fs.readFileSync(path.join(temp, "README.md"), "utf8"); + expect(readme).toStartWith("# " + path.basename(temp).toLowerCase() + "\n"); + expect(readme).toInclude("v" + Bun.version.replaceAll("-debug", "")); + expect(readme).toInclude("index.ts"); + }); + + it.each(filesCreated)("creates %s", file => { + expect(fs.existsSync(path.join(temp, file))).toBeTrue(); + }); +}); + +describe("`bun init` wth piped cli", () => { + let temp: string; + let out: SyncSubprocess; + + beforeAll(() => { + temp = tmpdirSync(); + out = Bun.spawnSync({ + cmd: [bunExe(), "init"], + cwd: temp, + stdio: [new Blob(["\n".repeat(12)]), "ignore", "inherit"], + env: bunEnv, + }); + }); + + afterAll(() => { + Bun.$`rm -rf ${temp}`.nothrow(); + }); + + it("exits successfully", () => { + expect(out).not.toHaveProperty("signal"); + expect(out.exitCode).toBe(0); + }); + + it("creates the expected package.json", () => { + const pkg = JSON.parse(fs.readFileSync(path.join(temp, "package.json"), "utf8")); + const expected = defaultPackageJson({ name: path.basename(temp).toLowerCase() }); + expect(pkg).toEqual(expected); + }); + + it("populates the README.md template", () => { + const readme = fs.readFileSync(path.join(temp, "README.md"), "utf8"); + expect(readme).toStartWith("# " + path.basename(temp).toLowerCase() + "\n"); + expect(readme).toInclude("v" + Bun.version.replaceAll("-debug", "")); + expect(readme).toInclude("index.ts"); + }); + + it.each(filesCreated)("creates %s", file => { + expect(fs.existsSync(path.join(temp, file))).toBeTrue(); + }); +});