diff --git a/src/index.test.ts b/src/index.test.ts index 566d80f..7f719fc 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1158,7 +1158,7 @@ test('regression #245: superjson referential equalities only use the top-most pa expect(parsed).toEqual(input); }); -test('dedupe=true', () => { +test('dedupe=true, pruned', () => { const instance = new SuperJSON({ dedupe: true, }); @@ -1180,14 +1180,48 @@ test('dedupe=true', () => { expect(json.a); // This has already been seen and should be deduped - expect(json.b).toBeNull(); + expect(json.b).toEqual('[Pruned *]'); expect(json).toMatchInlineSnapshot(` Object { "a": Object { "children": Array [], }, - "b": null, + "b": "[Pruned *]", + } + `); + + expect(instance.deserialize(output)).toEqual(input); +}); + +test('dedupe=true, circular', () => { + const instance = new SuperJSON({ + dedupe: true, + }); + + type Node = { + children: Node[]; + }; + const root: Node = { + children: [], + }; + root.children.push(root); + const input = { + a: root, + b: root, + }; + const output = instance.serialize(input); + + const json = output.json as any; + + expect(json).toMatchInlineSnapshot(` + Object { + "a": Object { + "children": Array [ + "[Circular *]", + ], + }, + "b": "[Pruned *]", } `); diff --git a/src/plainer.ts b/src/plainer.ts index 1a568df..e369bd4 100644 --- a/src/plainer.ts +++ b/src/plainer.ts @@ -169,7 +169,7 @@ export const walker = ( // short-circuit result if we've seen this object before return dedupe ? { - transformedValue: null, + transformedValue: '[Pruned *]', } : seen; } @@ -195,7 +195,7 @@ export const walker = ( if (includes(objectsInThisPath, object)) { // prevent circular references return { - transformedValue: null, + transformedValue: '[Circular *]', }; }