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
29 changes: 18 additions & 11 deletions src/runtime/test_runner/pretty_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1582,20 +1582,27 @@ impl<'a> Formatter<'a> {
}
return Ok(());
} else if bun_jsc::DOMFormData::from_js(value).is_some() {
let to_json_function = value.get(self.global_this, "toJSON")?.unwrap();
if let Some(to_json_function) = value
.get(self.global_this, "toJSON")?
.filter(|f| f.is_callable())
{
self.add_for_new_line(b"FormData (entries) ".len());
writer.write_all(
pretty_fmt_const::<ENABLE_ANSI_COLORS>(
"<r><blue>FormData<r> <d>(entries)<r> ",
)
.as_bytes(),
);

self.add_for_new_line(b"FormData (entries) ".len());
writer.write_all(
pretty_fmt_const::<ENABLE_ANSI_COLORS>(
"<r><blue>FormData<r> <d>(entries)<r> ",
)
.as_bytes(),
);
return self.print_as::<W, { Tag::Object }, ENABLE_ANSI_COLORS>(
writer.ctx,
to_json_function.call(self.global_this, value, &[])?,
JSType::Object,
);
}

return self.print_as::<W, { Tag::Object }, ENABLE_ANSI_COLORS>(
writer.ctx,
to_json_function.call(self.global_this, value, &[])?,
JSType::Object,
writer.ctx, value, JSType::Event,
);
} else if let Some(timer) = value.as_class_ref::<crate::timer::TimeoutObject>() {
self.add_for_new_line(
Expand Down
47 changes: 47 additions & 0 deletions test/js/bun/test/expect-formdata-tojson-crash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe, tempDir } from "harness";

test("failing matcher on FormData without a callable toJSON does not abort the test runner", async () => {
using dir = tempDir("formdata-tojson-crash", {
"formdata.test.ts": `
import { test, expect } from "bun:test";

test("toJSON shadowed with undefined", () => {
const fd = new FormData();
Object.defineProperty(fd, "toJSON", { value: undefined });
expect(fd).toEqual(1 as any);
});

test("null prototype", () => {
const fd = new FormData();
fd.append("a", "b");
Object.setPrototypeOf(fd, null);
expect(fd).toEqual(1 as any);
});

test("toJSON is a non-callable value", () => {
const fd = new FormData();
fd.append("a", "b");
Object.defineProperty(fd, "toJSON", { value: 42 });
expect(fd).toEqual(1 as any);
});
`,
Comment thread
claude[bot] marked this conversation as resolved.
});

await using proc = Bun.spawn({
cmd: [bunExe(), "test", "formdata.test.ts"],
env: bunEnv,
cwd: String(dir),
stdout: "pipe",
stderr: "pipe",
});

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

expect(stderr).toContain("Received: FormData");
// The non-callable toJSON is printed as a property rather than being called.
expect(stderr).toContain(`"toJSON": 42`);
expect(stderr).not.toContain("is not a function");
expect(stderr).toContain("3 fail");
expect(exitCode).toBe(1);
});