8

I'm looking for a way to test a React.Suspense fallback using react-testing-library. Consider this example:

const MyLazyThing = lazy(() => import('./index'));

export default function MyThing(props) {
  return (
    <Suspense fallback="loading...">
      <MyLazyThing {...props} />
    </Suspense>
  );
}

using getByText('loading...') doesn't seem to work. wondering if there's a good way to test this in RTL.

thx

Ben
  • 16,124
  • 22
  • 77
  • 122
  • Have you tried mocking React.Lazy to prevent it from loading the component? Something kind of like `jest.spyOn(React, 'lazy').mockImplementation(() => () => false)` – Charles Desbiens Aug 20 '20 at 01:07
  • 3
    I would add though that this test is a bit unnecessary. You're making a test to test that Suspense and Lazy work. It's generally safe to assume that React's features work. – Charles Desbiens Aug 20 '20 at 01:10

0 Answers0