diff --git a/packages/use-sync-external-store/src/__tests__/useSyncExternalStoreShared-test.js b/packages/use-sync-external-store/src/__tests__/useSyncExternalStoreShared-test.js index 5b0ce4f1a4..225c397bbd 100644 --- a/packages/use-sync-external-store/src/__tests__/useSyncExternalStoreShared-test.js +++ b/packages/use-sync-external-store/src/__tests__/useSyncExternalStoreShared-test.js @@ -900,7 +900,12 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => { it('selector can throw on update', async () => { const store = createExternalStore({a: 'a'}); - const selector = state => state.a.toUpperCase(); + const selector = state => { + if (typeof state.a !== 'string') { + throw new TypeError('Malformed state'); + } + return state.a.toUpperCase(); + }; function App() { const a = useSyncExternalStoreWithSelector( @@ -927,15 +932,18 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => { await act(() => { store.set({}); }); - expect(container.textContent).toEqual( - "Cannot read property 'toUpperCase' of undefined", - ); + expect(container.textContent).toEqual('Malformed state'); }); it('isEqual can throw on update', async () => { const store = createExternalStore({a: 'A'}); const selector = state => state.a; - const isEqual = (left, right) => left.a.trim() === right.a.trim(); + const isEqual = (left, right) => { + if (typeof left.a !== 'string' || typeof right.a !== 'string') { + throw new TypeError('Malformed state'); + } + return left.a.trim() === right.a.trim(); + }; function App() { const a = useSyncExternalStoreWithSelector( @@ -963,9 +971,7 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => { await act(() => { store.set({}); }); - expect(container.textContent).toEqual( - "Cannot read property 'trim' of undefined", - ); + expect(container.textContent).toEqual('Malformed state'); }); }); });