-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(cli/init): refactor init.test.ts
- Loading branch information
Showing
1 changed file
with
93 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |