diff --git a/src/bun.js/bindings/SnapshotSerializers.cpp b/src/bun.js/bindings/SnapshotSerializers.cpp index 570207c821..f5f2386b14 100644 --- a/src/bun.js/bindings/SnapshotSerializers.cpp +++ b/src/bun.js/bindings/SnapshotSerializers.cpp @@ -157,27 +157,34 @@ JSValue SnapshotSerializers::serialize(JSGlobalObject* globalObject, JSValue val JSValue testResult = call(globalObject, testCallback, callData, jsUndefined(), args); RETURN_IF_EXCEPTION(scope, {}); - // If the test returns truthy, use this serializer - if (testResult.toBoolean(globalObject)) { - JSValue serializeCallback = serializeCallbacks->getIndex(globalObject, static_cast(i)); - RETURN_IF_EXCEPTION(scope, {}); - - if (!serializeCallback.isCallable()) { - continue; - } - - // Call the serialize function with the value - auto serializeCallData = JSC::getCallData(serializeCallback); - MarkedArgumentBuffer serializeArgs; - serializeArgs.append(value); - ASSERT(!serializeArgs.hasOverflowed()); - - JSValue result = call(globalObject, serializeCallback, serializeCallData, jsUndefined(), serializeArgs); - RETURN_IF_EXCEPTION(scope, {}); - - // Return the serialized result (should be a string or null) - RELEASE_AND_RETURN(scope, result); + // If the test returns falsey, continue + if (!testResult.toBoolean(globalObject)) { + continue; } + + // Use this serializer + JSValue serializeCallback = serializeCallbacks->getIndex(globalObject, static_cast(i)); + RETURN_IF_EXCEPTION(scope, {}); + + if (!serializeCallback.isCallable()) { + continue; + } + + // Call the serialize function with the value + auto serializeCallData = JSC::getCallData(serializeCallback); + MarkedArgumentBuffer serializeArgs; + serializeArgs.append(value); + ASSERT(!serializeArgs.hasOverflowed()); + + JSValue result = call(globalObject, serializeCallback, serializeCallData, jsUndefined(), serializeArgs); + RETURN_IF_EXCEPTION(scope, {}); + + // Error if the result is not a string + if (!result.isString()) { + throwTypeError(globalObject, scope, "Snapshot serializer serialize callback must return a string"_s); + RELEASE_AND_RETURN(scope, {}); + } + RELEASE_AND_RETURN(scope, result); } // No matching serializer found diff --git a/src/bun.js/test/pretty_format.zig b/src/bun.js/test/pretty_format.zig index f554ef8aa5..60a6f9a413 100644 --- a/src/bun.js/test/pretty_format.zig +++ b/src/bun.js/test/pretty_format.zig @@ -876,11 +876,7 @@ pub const JestPrettyFormat = struct { const result = try bun.cpp.SnapshotSerializers__serialize(this.globalThis, serializers, value); if (!result.isNull()) { // Serializer matched but returned non-string value - if (!result.isString()) { - var formatter = jsc.ConsoleObject.Formatter{ .globalThis = this.globalThis }; - defer formatter.deinit(); - return this.globalThis.throw("Snapshot serializer must return a string, received: {any}", .{result.toFmt(&formatter)}); - } + if (bun.Environment.ci_assert) bun.assert(result.isString()); var writer = WrappedWriter(Writer){ .ctx = writer_, .estimated_line_length = &this.estimated_line_length }; defer { diff --git a/test/js/bun/test/snapshot-serializers.test.ts b/test/js/bun/test/snapshot-serializers.test.ts index 94f3ba195e..6924a51c6b 100644 --- a/test/js/bun/test/snapshot-serializers.test.ts +++ b/test/js/bun/test/snapshot-serializers.test.ts @@ -121,5 +121,5 @@ test("serialize function returning non-string throws error", () => { const obj = new ReturnNonString(); expect(() => { expect(obj).toMatchInlineSnapshot(); - }).toThrow("Snapshot serializer must return a string"); + }).toThrow("Snapshot serializer serialize callback must return a string"); });