Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/bun.js/ConsoleObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,7 @@ pub const Formatter = struct {
o, // o
O, // O
c, // c
j, // j
};

fn writeWithFormatting(
Expand Down Expand Up @@ -1466,6 +1467,7 @@ pub const Formatter = struct {
'O' => .O,
'd', 'i' => .i,
'c' => .c,
'j' => .j,
'%' => {
// print up to and including the first %
const end = slice[0..i];
Expand Down Expand Up @@ -1625,6 +1627,16 @@ pub const Formatter = struct {
.c => {
// TODO: Implement %c
},

.j => {
// JSON.stringify the value
var str = bun.String.empty;
defer str.deref();

try next_value.jsonStringify(global, 0, &str);
this.addForNewLine(str.length());
writer.print("{f}", .{str});
},
}
if (this.remaining_values.len == 0) break;
},
Expand Down
2 changes: 1 addition & 1 deletion test/js/web/console/console-log.expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ Hello World 123
Hello %vWorld 123
Hello NaN %i
Hello NaN % 1
Hello NaN %j 1
Hello NaN 1
Hello \5 6,
Hello %i 5 6
%d 1
72 changes: 72 additions & 0 deletions test/regression/issue/24234.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe } from "harness";

test("console.log with %j should format as JSON", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", "console.log('%j', {foo: 'bar'})"],
env: bunEnv,
stderr: "pipe",
});

const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);

expect(stderr).toBe("");
expect(stdout).toBe('{"foo":"bar"}\n');
expect(exitCode).toBe(0);
});

test("console.log with %j should handle arrays", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", "console.log('%j', [1, 2, 3])"],
env: bunEnv,
stderr: "pipe",
});

const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);

expect(stderr).toBe("");
expect(stdout).toBe("[1,2,3]\n");
expect(exitCode).toBe(0);
});

test("console.log with %j should handle nested objects", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", "console.log('%j', {a: {b: {c: 123}}})"],
env: bunEnv,
stderr: "pipe",
});

const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);

expect(stderr).toBe("");
expect(stdout).toBe('{"a":{"b":{"c":123}}}\n');
expect(exitCode).toBe(0);
});

test("console.log with %j should handle primitives", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", "console.log('%j %j %j %j', 'string', 123, true, null)"],
env: bunEnv,
stderr: "pipe",
});

const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);

expect(stderr).toBe("");
expect(stdout).toBe('"string" 123 true null\n');
expect(exitCode).toBe(0);
});

test("console.log with %j and additional text", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", "console.log('Result: %j', {status: 'ok'})"],
env: bunEnv,
stderr: "pipe",
});

const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);

expect(stderr).toBe("");
expect(stdout).toBe('Result: {"status":"ok"}\n');
expect(exitCode).toBe(0);
});
Comment on lines +18 to +72
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial

Good test coverage for the regression issue.

The tests cover the key scenarios: objects, arrays, nested objects, primitives, and mixed formatting. This adequately validates the fix for issue #24234.

Consider adding edge case tests in a follow-up for completeness:

  • undefined value (outputs undefined in JSON, but you may want to verify)
  • Circular references (would throw or output something specific)
  • Non-serializable values like Symbol() or functions