Skip to content
Open
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
1 change: 1 addition & 0 deletions src/jsc/ConsoleObject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2089,6 +2089,7 @@ pub mod formatter {
| Tag::Error
| Tag::Class
| Tag::Event
| Tag::JSX
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/test_runner/pretty_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ impl Tag {

#[inline]
pub const fn can_have_circular_references(self) -> bool {
matches!(self, Tag::Array | Tag::Object | Tag::Map | Tag::Set)
matches!(self, Tag::Array | Tag::Object | Tag::Map | Tag::Set | Tag::JSX)
}
}

Expand Down
83 changes: 82 additions & 1 deletion test/js/bun/util/inspect.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from "bun:test";
import { normalizeBunSnapshot, tmpdirSync } from "harness";
import { bunEnv, bunExe, normalizeBunSnapshot, tempDir, tmpdirSync } from "harness";
import { join } from "path";
import util from "util";
it("prototype", () => {
Expand Down Expand Up @@ -316,6 +316,87 @@ it("jsx with fragment", () => {
expect(input).toBe(output);
});

it("jsx with circular references does not crash", () => {
// Run in a subprocess: without the fix this overflows the native stack and segfaults,
// which would otherwise kill the test runner before it can record a failure.
const { exitCode, stdout } = Bun.spawnSync({
cmd: [
bunExe(),
"-e",
`
const a = { $$typeof: Symbol.for("react.element"), type: "div", props: null, key: null };
a.props = a;
console.log(Bun.inspect(a));

const b = { $$typeof: Symbol.for("react.element"), type: "div", key: null };
b.props = { children: b };
console.log(Bun.inspect(b));

const c = { $$typeof: Symbol.for("react.element"), type: "span", key: null };
c.props = { children: [c, c] };
console.log(Bun.inspect(c));

const d = { $$typeof: Symbol.for("react.element"), type: "div", props: {} };
d.key = d;
console.log(Bun.inspect(d));
`,
],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const out = stdout.toString();
expect(out).toContain("[Circular]");
expect(out).toContain("<div>\n [Circular]\n</div>");
expect(out).toContain("<span>\n [Circular]\n [Circular]\n</span>");
expect(out).toContain("<div key=[Circular] />");
expect(exitCode).toBe(0);
});

it("jsx with non-object props does not crash", () => {
const { exitCode, stdout } = Bun.spawnSync({
cmd: [
bunExe(),
"-e",
`
const el = { $$typeof: Symbol.for("react.element"), type: "div", props: 42, key: null };
console.log(Bun.inspect(el));
`,
],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
expect(stdout.toString().trim()).toBe("<div />");
expect(exitCode).toBe(0);
});

it("jsx with circular props in test diff formatter", () => {
using dir = tempDir("jsx-circular-diff", {
"diff.test.js": `
import { test, expect } from "bun:test";
test("circular", () => {
const el = { $$typeof: Symbol.for("react.element"), type: "div", props: null, key: null };
el.props = el;
expect(() => expect(el).toEqual({})).toThrow();
});
test("non-object props", () => {
const el = { $$typeof: Symbol.for("react.element"), type: "div", props: 42, key: null };
expect(() => expect(el).toEqual({})).toThrow();
});
`,
});
const { exitCode, stderr } = Bun.spawnSync({
cmd: [bunExe(), "test", "diff.test.js"],
cwd: String(dir),
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
expect(stderr.toString()).toContain("2 pass");
expect(exitCode).toBe(0);
});

it("inspect", () => {
expect(Bun.inspect(new TypeError("what")).includes("TypeError: what")).toBe(true);
expect(Bun.inspect("hi")).toBe('"hi"');
Expand Down
Loading