test: Throw custom error instead of relying on runtime error (#24946)
Sebastian Silbermann committed
Jul 21, 2022 at 21:46 UTC
6b28bc9c5aa91e1d848c97c380e284d3f518b3bd
1 file changed
+14
-8
packages/use-sync-external-store/src/__tests__/useSyncExternalStoreShared-test.js
+14
-8
@@ -900,7 +900,12 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
900
901
it('selector can throw on update', async () => {
902
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
+ };
909
910
function App() {
911
const a = useSyncExternalStoreWithSelector(
@@ -927,15 +932,18 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
932
await act(() => {
933
store.set({});
934
});
930
- expect(container.textContent).toEqual(
931
- "Cannot read property 'toUpperCase' of undefined",
932
- );
935
+ expect(container.textContent).toEqual('Malformed state');
936
});
937
938
it('isEqual can throw on update', async () => {
939
const store = createExternalStore({a: 'A'});
940
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
+ };
947
948
function App() {
949
const a = useSyncExternalStoreWithSelector(
@@ -963,9 +971,7 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
971
await act(() => {
972
store.set({});
973
});
966
- expect(container.textContent).toEqual(
967
- "Cannot read property 'trim' of undefined",
968
- );
974
+ expect(container.textContent).toEqual('Malformed state');
975
});
976
});
977
});