I have to components imported with the new React lazy
API (16.6).
import React, {PureComponent, lazy} from 'react';
const Component1 = lazy(() => import('./Component1'));
const Component2 = lazy(() => import('./Component2'));
class CustomComponent extends PureComponent {
...
render() {
return (
<div>
<Component1 />
<Component2 />
</div>
);
}
}
In my tests, I'm doing the snapshots of this component. It's a very straightforward test:
import { create } from 'react-test-renderer';
const tree = await create(<CustomComponent />).toJSON();
expect(tree).toMatchSnapshot();
In the logs, the test is failing with this error:
A React component suspended while rendering, but no fallback UI was specified.
Add a <Suspense fallback=...> component higher in the tree to provide a loading indicator or placeholder to display.
Do I have to wrap in every single test suite with <Suspense>...
?
it('should show the component', async () => {
const component = await create(
<React.Suspense fallback={<div>loading</div>}>
<CustomComponent />
</React.Suspense>
);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
};
If I do that, I only see in the snapshot the fallback
component.
+ Array [ + <div> + loading + </div>, + ]
So, which is the best way to do it?
Do I have to wrap in every single test suite with
<Suspense>
?Export
Component1
andComponent2
inCustomComponent
so that they can be imported in tests.Remember that the lazy loaded components are promise-like. Import them in the test, and wait for them to resolve before doing a check that the snapshot matches.