This commit is contained in:
pfg
2025-11-17 20:06:12 -08:00
parent 2b81198922
commit 0cfd29fcf3

View File

@@ -18,6 +18,14 @@ class Size {
) {}
}
class ThrowInTest {
value = 42;
}
class ThrowInSerialize {
value = 99;
}
// Add serializers at the top level
expect.addSnapshotSerializer({
test: val => val instanceof Point,
@@ -40,6 +48,23 @@ expect.addSnapshotSerializer({
print: val => `Size{${val.width}x${val.height}}`,
});
expect.addSnapshotSerializer({
test: val => {
if (val instanceof ThrowInTest) {
throw new Error("Test function error");
}
return false;
},
serialize: val => `ThrowInTest(${val.value})`,
});
expect.addSnapshotSerializer({
test: val => val instanceof ThrowInSerialize,
serialize: () => {
throw new Error("Serialize function error");
},
});
test("snapshot serializers work for custom formatting", () => {
const color = new Color("red");
expect(color).toMatchInlineSnapshot(`Color[red]`);
@@ -70,20 +95,6 @@ test("snapshot serializers apply to object fields", () => {
});
test("test function throwing error propagates to expect()", () => {
class ThrowInTest {
value = 42;
}
expect.addSnapshotSerializer({
test: val => {
if (val instanceof ThrowInTest) {
throw new Error("Test function error");
}
return false;
},
serialize: val => `ThrowInTest(${val.value})`,
});
const obj = new ThrowInTest();
expect(() => {
expect(obj).toMatchInlineSnapshot();
@@ -91,17 +102,6 @@ test("test function throwing error propagates to expect()", () => {
});
test("serialize function throwing error propagates to expect()", () => {
class ThrowInSerialize {
value = 99;
}
expect.addSnapshotSerializer({
test: val => val instanceof ThrowInSerialize,
serialize: () => {
throw new Error("Serialize function error");
},
});
const obj = new ThrowInSerialize();
expect(() => {
expect(obj).toMatchInlineSnapshot();