Skip to content

Commit

Permalink
feat: simple test fixture for cli and tsci init test (#22)
Browse files Browse the repository at this point in the history
* feat: simple test fixture for cli and tsci init test

* fix: format

* fix: use afterEach from bun:test
  • Loading branch information
kom-senapati authored Jan 16, 2025
1 parent c0475ca commit 58ce2e0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/fixtures/get-cli-test-fixture.ts
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,
}
}
20 changes: 20 additions & 0 deletions tests/test2-cli-init.test.ts
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",
]
`)
})

0 comments on commit 58ce2e0

Please sign in to comment.