paralleldrive / riteway

Simple, readable, helpful unit tests.
MIT License
1.15k stars 35 forks source link

Using Try with render seems to stop execution of other tests #316

Open dshenderson opened 3 years ago

dshenderson commented 3 years ago

I have created a useStore consumer hook to access context in my TypeScript React app following the pattern recommended by Kent C. Dodds:

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.