diff --git a/.changeset/hip-jars-report.md b/.changeset/hip-jars-report.md new file mode 100644 index 0000000000..f66e91d583 --- /dev/null +++ b/.changeset/hip-jars-report.md @@ -0,0 +1,5 @@ +--- +"@lynx-js/react": patch +--- + +Optimize the error message when snapshots cannot be found in the main thread. diff --git a/packages/react/runtime/__test__/snapshotPatch.test.jsx b/packages/react/runtime/__test__/snapshotPatch.test.jsx index 8c498ef6ff..7ce1c0b7b5 100644 --- a/packages/react/runtime/__test__/snapshotPatch.test.jsx +++ b/packages/react/runtime/__test__/snapshotPatch.test.jsx @@ -1315,3 +1315,17 @@ describe('list', () => { `); }); }); + +describe('missing snapshot', () => { + beforeEach(() => { + initGlobalSnapshotPatch(); + }); + + it('should throw error when missing snapshot', () => { + const bsi1 = new BackgroundSnapshotInstance('missing-snapshot'); + let patch; + patch = takeGlobalSnapshotPatch(); + expect(patch.length).toMatchInlineSnapshot(`3`); + expect(() => snapshotPatchApply(patch)).toThrowError('Snapshot not found: missing-snapshot'); + }); +}); diff --git a/packages/react/runtime/src/snapshot.ts b/packages/react/runtime/src/snapshot.ts index 51f63f864a..53d1b51d27 100644 --- a/packages/react/runtime/src/snapshot.ts +++ b/packages/react/runtime/src/snapshot.ts @@ -275,6 +275,10 @@ export class SnapshotInstance { constructor(public type: string, id?: number) { this.__snapshot_def = snapshotManager.values.get(type)!; + // Suspense uses 'div' + if (!this.__snapshot_def && type !== 'div') { + throw new Error('Snapshot not found: ' + type); + } id ||= snapshotInstanceManager.nextId -= 1; this.__id = id;