Skip to content

Commit

Permalink
test(cli/init): refactor init.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Jan 11, 2025
1 parent a81b130 commit 05dd987
Showing 1 changed file with 93 additions and 67 deletions.
160 changes: 93 additions & 67 deletions test/cli/init/init.test.ts
Original file line number Diff line number Diff line change
@@ -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,
});
});

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");
});

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.each(filesCreated)("creates %s", file => {
expect(fs.existsSync(path.join(temp, file))).toBeTrue();
});
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,
});

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");
});

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.each(filesCreated)("creates %s", file => {
expect(fs.existsSync(path.join(temp, file))).toBeTrue();
});
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);
});

0 comments on commit 05dd987

Please sign in to comment.