import { file, spawn } from "bun"; import { expect, it } from "bun:test"; import { bunEnv, bunExe } from "harness"; import { join } from "node:path"; it("should log to console correctly", async () => { const { stdout, stderr, exited } = spawn({ cmd: [bunExe(), join(import.meta.dir, "console-log.js")], stdin: "inherit", stdout: "pipe", stderr: "pipe", env: bunEnv, }); const exitCode = await exited; const err = (await new Response(stderr).text()).replaceAll("\r\n", "\n"); const out = (await new Response(stdout).text()).replaceAll("\r\n", "\n"); const expected = (await new Response(file(join(import.meta.dir, "console-log.expected.txt"))).text()).replaceAll( "\r\n", "\n", ); const errMatch = err === "uh oh\n"; const outmatch = out === expected; if (errMatch && outmatch && exitCode === 0) { expect().pass(); return; } console.error(err); console.log("Length of output:", out.length); console.log("Length of expected:", expected.length); console.log("Exit code:", exitCode); expect(out).toBe(expected); expect(err).toBe("uh oh\n"); expect(exitCode).toBe(0); }); it("long arrays get cutoff", () => { const proc = Bun.spawnSync({ cmd: [bunExe(), "-e", `console.log(Array(1000).fill(0))`], env: bunEnv, stdio: ["inherit", "pipe", "pipe"], }); expect(proc.exitCode).toBe(0); expect(proc.stderr.toString("utf8")).toBeEmpty(); expect(proc.stdout.toString("utf8")).toEqual( "[\n" + " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n" + " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n" + " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n" + " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n" + " ... 900 more items\n" + "]\n" + "", ); });