Files
bun.sh/test/regression/issue/21257.test.ts
robobun b1f83d0bb2 fix: Response.json() throws TypeError for non-JSON serializable top-level values (#21258)
Co-authored-by: Claude Bot <claude-bot@bun.sh>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Meghan Denny <meghan@bun.com>
2025-10-20 19:46:22 -07:00

33 lines
1.5 KiB
TypeScript

// Regression test for GitHub Issue #21257
// https://github.com/oven-sh/bun/issues/21257
// `Response.json()` should throw with top level value of `function` `symbol` `undefined` (node compatibility)
import { expect, test } from "bun:test";
test("Response.json() throws TypeError for non-JSON serializable top-level values", () => {
// These should throw "Value is not JSON serializable"
expect(() => Response.json(Symbol("test"))).toThrow("Value is not JSON serializable");
expect(() => Response.json(function testFunc() {})).toThrow("Value is not JSON serializable");
expect(() => Response.json(undefined)).toThrow("Value is not JSON serializable");
});
test("Response.json() works correctly with valid values", () => {
// These should not throw
expect(() => Response.json(null)).not.toThrow();
expect(() => Response.json({})).not.toThrow();
expect(() => Response.json("string")).not.toThrow();
expect(() => Response.json(123)).not.toThrow();
expect(() => Response.json(true)).not.toThrow();
expect(() => Response.json([1, 2, 3])).not.toThrow();
// Objects containing non-serializable values should not throw at top-level
expect(() => Response.json({ symbol: Symbol("test") })).not.toThrow();
expect(() => Response.json({ func: function () {} })).not.toThrow();
expect(() => Response.json({ undef: undefined })).not.toThrow();
});
test("Response.json() BigInt error matches Node.js", () => {
// BigInt should throw with Node.js compatible error message
expect(() => Response.json(123n)).toThrow("Do not know how to serialize a BigInt");
});