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
21 changes: 21 additions & 0 deletions src/jsc/ConsoleObject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5707,6 +5707,27 @@ pub mod formatter {
}
self.print_as::<ENABLE_ANSI_COLORS>(result.tag.tag(), writer, value, result.cell)
}

/// Format a single value into `writer`, propagating a JS exception
/// thrown while inspecting it (e.g. a throwing `[inspect.custom]`).
/// Use this instead of the `Display` adapter ([`ZigFormatter`]) when a
/// `JsResult` caller needs the error: `Display` can only report
/// `fmt::Error`, which panics inside `io::Write::write_fmt` when the
/// sink itself did not fail.
Comment thread
robobun marked this conversation as resolved.
pub fn format_value<const ENABLE_ANSI_COLORS: bool>(
&mut self,
value: JSValue,
writer: &mut dyn bun_io::Write,
) -> JsResult<()> {
self.stack_check.update();
let one = [value];
self.remaining_values = bun_ptr::RawSlice::new(&one);
let global = self.global_this;
let result = Tag::get(value, global)
.and_then(|tag| self.format::<ENABLE_ANSI_COLORS>(tag, writer, value, global));
self.remaining_values = bun_ptr::RawSlice::EMPTY;
result
}
}

/// Abstracts over `{d}` vs `{f}` and `n`-suffix for `write_typed_array`.
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();
formatter.format_value::<false>(value, &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();
formatter.format_value::<false>(current_arg, &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