-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: simple test fixture for cli and tsci init test (#22)
* feat: simple test fixture for cli and tsci init test * fix: format * fix: use afterEach from bun:test
- Loading branch information
1 parent
c0475ca
commit 58ce2e0
Showing
2 changed files
with
62 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { mkdtemp, rm } from "node:fs/promises" | ||
import { join, resolve } from "node:path" | ||
import { tmpdir } from "node:os" | ||
import { exec } from "node:child_process" | ||
import { promisify } from "node:util" | ||
import { afterEach } from "bun:test" | ||
|
||
const execAsync = promisify(exec) | ||
|
||
export interface CliTestFixture { | ||
tmpDir: string | ||
runCommand: (command: string) => Promise<{ stdout: string; stderr: string }> | ||
} | ||
|
||
export async function getCliTestFixture(): Promise<CliTestFixture> { | ||
const tmpDir = await mkdtemp(join(tmpdir(), "tsci-test-")) | ||
|
||
const runCommand = async (command: string) => { | ||
// Convert command like "tsci init" to ["cli/main.ts", "init"] | ||
const args = command.split(" ") | ||
if (args[0] !== "tsci") { | ||
throw new Error(`Expected command to start with "tsci", got: ${command}`) | ||
} | ||
args[0] = resolve(process.cwd(), "cli/main.ts") | ||
|
||
const result = await execAsync(`bun ${args.join(" ")}`, { | ||
cwd: tmpDir, | ||
}) | ||
return result | ||
} | ||
|
||
const cleanup = async () => { | ||
await rm(tmpDir, { recursive: true, force: true }) | ||
} | ||
|
||
afterEach(cleanup) | ||
|
||
return { | ||
tmpDir, | ||
runCommand, | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { getCliTestFixture } from "./fixtures/get-cli-test-fixture" | ||
import { test, expect } from "bun:test" | ||
import { readdir } from "node:fs/promises" | ||
|
||
test("basic init", async () => { | ||
const { tmpDir, runCommand } = await getCliTestFixture() | ||
|
||
// Run the `tsci init` command | ||
const { stdout, stderr } = await runCommand("tsci init") | ||
expect(stderr).toBe("") | ||
|
||
// List directory contents | ||
const dirContents = await readdir(tmpDir) | ||
expect(dirContents).toMatchInlineSnapshot(` | ||
[ | ||
".npmrc", | ||
"index.tsx", | ||
] | ||
`) | ||
}) |