import { spawn } from "bun"; import { describe, expect, test } from "bun:test"; import { bunEnv, bunExe } from "harness"; import { join } from "node:path"; describe("node:test", () => { test("should run basic tests", async () => { const { exitCode, stderr } = await runTests(["01-harness.js"]); expect({ exitCode, stderr }).toMatchObject({ exitCode: 0, stderr: expect.stringContaining("0 fail"), }); }); test("should run hooks in the right order", async () => { const { exitCode, stderr } = await runTests(["02-hooks.js"]); expect({ exitCode, stderr }).toMatchObject({ exitCode: 0, stderr: expect.stringContaining("0 fail"), }); }); test("should run tests with different variations", async () => { const { exitCode, stderr } = await runTests(["03-test-variations.js"]); expect({ exitCode, stderr }).toMatchObject({ exitCode: 0, stderr: expect.stringContaining("0 fail"), }); }); test("should run async tests", async () => { const { exitCode, stderr } = await runTests(["04-async-tests.js"]); expect({ exitCode, stderr }).toMatchObject({ exitCode: 0, stderr: expect.stringContaining("0 fail"), }); }); test("should run all tests from multiple files", async () => { const { exitCode, stderr } = await runTests(["01-harness.js", "02-hooks.js"]); expect({ exitCode, stderr }).toMatchObject({ exitCode: 0, // 32 from 01-harness + 3 from 02-hooks stderr: expect.stringContaining("35 pass"), }); }); test("should throw NotImplementedError if you call test() or describe() inside another test()", async () => { const { exitCode, stderr } = await runTests(["05-test-in-test.js"]); expect({ exitCode, stderr }).toMatchObject({ exitCode: 0, stderr: expect.stringContaining("0 fail"), }); }); }); async function runTests(filenames: string[]) { const testPaths = filenames.map(filename => join(import.meta.dirname, "fixtures", filename)); const { exited, stdout: stdoutStream, stderr: stderrStream, } = spawn({ cmd: [bunExe(), "test", ...testPaths], env: bunEnv, stderr: "pipe", }); const [exitCode, stdout, stderr] = await Promise.all([ exited, new Response(stdoutStream).text(), new Response(stderrStream).text(), ]); return { exitCode, stdout, stderr }; }