Skip to content

Commit 6b28bc9

Browse files
authored
test: Throw custom error instead of relying on runtime error (#24946)
1 parent 9bd0dd4 commit 6b28bc9

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

packages/use-sync-external-store/src/__tests__/useSyncExternalStoreShared-test.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,12 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
900900

901901
it('selector can throw on update', async () => {
902902
const store = createExternalStore({a: 'a'});
903-
const selector = state => state.a.toUpperCase();
903+
const selector = state => {
904+
if (typeof state.a !== 'string') {
905+
throw new TypeError('Malformed state');
906+
}
907+
return state.a.toUpperCase();
908+
};
904909

905910
function App() {
906911
const a = useSyncExternalStoreWithSelector(
@@ -927,15 +932,18 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
927932
await act(() => {
928933
store.set({});
929934
});
930-
expect(container.textContent).toEqual(
931-
"Cannot read property 'toUpperCase' of undefined",
932-
);
935+
expect(container.textContent).toEqual('Malformed state');
933936
});
934937

935938
it('isEqual can throw on update', async () => {
936939
const store = createExternalStore({a: 'A'});
937940
const selector = state => state.a;
938-
const isEqual = (left, right) => left.a.trim() === right.a.trim();
941+
const isEqual = (left, right) => {
942+
if (typeof left.a !== 'string' || typeof right.a !== 'string') {
943+
throw new TypeError('Malformed state');
944+
}
945+
return left.a.trim() === right.a.trim();
946+
};
939947

940948
function App() {
941949
const a = useSyncExternalStoreWithSelector(
@@ -963,9 +971,7 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
963971
await act(() => {
964972
store.set({});
965973
});
966-
expect(container.textContent).toEqual(
967-
"Cannot read property 'trim' of undefined",
968-
);
974+
expect(container.textContent).toEqual('Malformed state');
969975
});
970976
});
971977
});

0 commit comments

Comments
 (0)