Files
bun.sh/test/js/web/console/console-log.test.ts
2024-11-06 19:40:58 -08:00

70 lines
2.3 KiB
TypeScript

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" +
"",
);
});
it("console.group", async () => {
const proc = Bun.spawnSync({
cmd: [bunExe(), join(import.meta.dir, "console-group.fixture.js")],
env: bunEnv,
stdio: ["inherit", "pipe", "pipe"],
});
expect(proc.exitCode).toBe(0);
expect(proc.stderr.toString("utf8").replaceAll("\r\n", "\n").trim()).toMatchSnapshot("console-group-error");
expect(proc.stdout.toString("utf8").replaceAll("\r\n", "\n").trim()).toMatchSnapshot("console-group-output");
});