This commit is contained in:
pfg
2025-11-18 18:44:21 -08:00
parent 52fbeee121
commit e55ebfdbd0
3 changed files with 29 additions and 26 deletions

View File

@@ -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<unsigned>(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<unsigned>(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

View File

@@ -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 {

View File

@@ -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");
});