Files
bun.sh/test/js/bun/runtime/source-code-preview.test.ts
Claude Bot a665db4549 test: improve source code preview test to verify console.log not in output
Update test to:
- Throw actual errors instead of console.log(stack) to trigger source preview
- Check that source code lines with 'console.log' don't appear in output
- Verify source code preview shows line numbers and pipes in default mode
- Verify caret indicators (^) are present in default mode
- Ensure source code lines and carets are NOT present when disabled

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-16 03:55:41 +00:00

89 lines
2.5 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { bunEnv, bunExe, tempDir } from "harness";
describe("sourceCodePreview config option", () => {
test("default behavior shows source code in error stack traces", async () => {
using dir = tempDir("source-code-preview-default", {
"test.js": `
function foo() {
throw new Error("Test error");
}
foo();
`,
});
const proc = Bun.spawn({
cmd: [bunExe(), "test.js"],
cwd: String(dir),
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const stderr = await proc.stderr.text();
const exitCode = await proc.exited;
expect(exitCode).toBe(1);
// Should contain file path and line numbers
expect(stderr).toContain("test.js:");
// Should contain source code preview with line numbers and pipes
expect(stderr).toMatch(/\d+\s+\|/);
// Should contain the source line with "throw new Error"
expect(stderr).toContain('throw new Error("Test error")');
// Should contain caret indicator
expect(stderr).toContain("^");
});
test("sourceCodePreview=false disables source code in error stack traces", async () => {
using dir = tempDir("source-code-preview-disabled", {
"bunfig.toml": `
[runtime]
sourceCodePreview = false
`,
"test.fixture.ts": `
function foo() {
console.log("before error");
throw new Error("Test error");
}
foo();
`,
});
const proc2 = Bun.spawn({
cmd: [bunExe(), "test.fixture.ts"],
cwd: String(dir),
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const stdout = await proc2.stdout.text();
const stderr = await proc2.stderr.text();
const exitCode = await proc2.exited;
expect(exitCode).toBe(1);
// Should still contain file path and line numbers in stack trace
expect(stderr).toContain("test.fixture.ts:");
expect(stderr).toContain("Test error");
// Should NOT contain the console.log output (source code should not be displayed)
expect(stdout).not.toContain("console.log");
expect(stderr).not.toContain("console.log");
// Should NOT contain source code snippets (no pipe characters from source display)
// The source code preview shows lines like:
// 3 | throw new Error("Test error");
// ^
expect(stderr).not.toMatch(/\d+\s+\|/);
// Should NOT contain caret indicators
expect(stderr).not.toContain(" ^");
// Should NOT show the actual source line "throw new Error"
expect(stderr).not.toContain('throw new Error("Test error")');
});
});