export const useStore = () => {
const context = useContext(StoreContext);
if (context === undefined) {
throw new Error('useStore must be used within a StoreProvider');
}
return context;
};
I want to test that the error is thrown when context is undefined, so I've written the following test using render and Try:
import React, { ReactElement } from 'react';
import { describe, Try } from 'riteway';
import render from 'riteway/render-component';
import { useStore } from './Store.provider';
const ContextlessComponent = (): ReactElement => {
const { state } = useStore();
return <>{state.toString()}</>;
};
describe('Store/Store.provider/useStore()', async (assert) => {
assert({
given: 'useStore() is called without a StoreProvider',
should: 'throw an error',
actual: Try(render, <ContextlessComponent />).message,
expected: 'useStore must be used within a StoreProvider',
});
});
The test passes, but no further tests get run after this one.
I have created a
useStore
consumer hook to access context in my TypeScript React app following the pattern recommended by Kent C. Dodds:I want to test that the error is thrown when
context
isundefined
, so I've written the following test usingrender
andTry
:The test passes, but no further tests get run after this one.