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

impl ZigFormatter<'_, '_> {
/// Like the `Display` impl below, but propagates the real `JsError`
/// (e.g. a throwing `[util.inspect.custom]`) instead of collapsing it
/// to `fmt::Error`.
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
}
}

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
5 changes: 2 additions & 3 deletions src/runtime/test_runner/jest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ pub(crate) fn format_label(
} 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();
value.to_fmt(&mut formatter).write_to(&mut list)?;
}
idx = var_end;
continue;
Expand Down Expand Up @@ -787,8 +787,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
Loading