Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 28 additions & 0 deletions src/jsc/ConsoleObject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,34 @@ pub mod formatter {
}
}

impl ZigFormatter<'_, '_> {
/// Format into a byte sink, propagating the real `JsError` (a throwing
/// `[util.inspect.custom]`, termination, OOM). The `Display` impl below
/// collapses that to `fmt::Error`, which `std::io::Write::write_fmt`
/// turns into a panic when the sink itself never errored; use this
/// wherever the caller can `?`-propagate instead.
Comment thread
robobun marked this conversation as resolved.
Outdated
pub fn write_to(&self, writer: &mut dyn bun_io::Write) -> JsResult<()> {
let formatter: &mut Formatter<'_> = self
.formatter
.take()
.expect("ZigFormatter::write_to re-entered or used after consumption");

formatter.stack_check.update();
let one = [self.value];
formatter.remaining_values = bun_ptr::RawSlice::new(&one);

let result = (|| {
let tag = Tag::get(self.value, formatter.global_this)?;
let global = formatter.global_this;
formatter.format::<false>(tag, writer, self.value, global)
})();

formatter.remaining_values = bun_ptr::RawSlice::EMPTY;
self.formatter.set(Some(formatter));
result
}
}
Comment thread
robobun marked this conversation as resolved.
Outdated

impl core::fmt::Display for ZigFormatter<'_, '_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
// Move the unique `&mut Formatter` out of the cell for the body;
Expand Down
9 changes: 5 additions & 4 deletions src/runtime/test_runner/jest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,8 +709,10 @@ pub(crate) fn format_label(
list.extend_from_slice(owned_slice.slice());
} else {
let mut formatter = crate::test_runner::expect::make_formatter(global_this);
// formatter cleanup handled by Drop.
write!(&mut list, "{}", value.to_fmt(&mut formatter)).unwrap();
// formatter cleanup handled by Drop. `write_to` (not
// `write!`) so a throwing custom formatter surfaces as
// a JS error instead of a panic in `write_fmt`.
Comment thread
robobun marked this conversation as resolved.
Outdated
value.to_fmt(&mut formatter).write_to(&mut list)?;
}
idx = var_end;
continue;
Expand Down Expand Up @@ -787,8 +789,7 @@ pub(crate) fn format_label(
}
b'p' => {
let mut formatter = crate::test_runner::expect::make_formatter(global_this);
let value_fmt = current_arg.to_fmt(&mut formatter);
write!(&mut list, "{}", value_fmt).unwrap();
current_arg.to_fmt(&mut formatter).write_to(&mut list)?;
idx += 1;
args_idx += 1;
}
Expand Down
50 changes: 50 additions & 0 deletions test/cli/test/bun-test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,56 @@ describe("bun test", () => {
expect(stderr).toContain("First user: Alice with tag: admin");
});

test("surfaces a throwing custom formatter in the interpolated value as a test error", () => {
// The declaration throw aborts module evaluation, so each variant
// needs its own file to be verified independently.
const throwing = (message: string) =>
`({ [Symbol.for("nodejs.util.inspect.custom")]() { throw new Error(${JSON.stringify(message)}); } })`;
const stderr = runTest({
args: [],
expectExitCode: 1,
input: [
{
filename: "test-each-path.test.ts",
contents: `
import { test } from "bun:test";
test.each([{ a: { b: ${throwing("boom from test.each $path")} } }])("case $a.b", () => {});
`,
},
{
filename: "test-each-p.test.ts",
contents: `
import { test } from "bun:test";
test.each([[${throwing("boom from test.each %p")}]])("case %p", () => {});
`,
},
{
filename: "describe-each-path.test.ts",
contents: `
import { test, describe } from "bun:test";
describe.each([{ a: { b: ${throwing("boom from describe.each $path")} } }])("suite $a.b", () => {
test("inner", () => {});
});
`,
},
{
filename: "describe-each-p.test.ts",
contents: `
import { test, describe } from "bun:test";
describe.each([[${throwing("boom from describe.each %p")}]])("suite %p", () => {
test("inner", () => {});
});
`,
},
],
});

expect(stderr).toContain("boom from test.each $path");
expect(stderr).toContain("boom from test.each %p");
expect(stderr).toContain("boom from describe.each $path");
expect(stderr).toContain("boom from describe.each %p");
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Comment thread
claude[bot] marked this conversation as resolved.

test("handles missing properties gracefully", () => {
const cases = [{ a: 1 }];

Expand Down