Skip to content

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

@dshenderson

Description

@dshenderson

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions