'@testing-library waitfor not finding element in lazy loaded component
I'm trying to use @testing-library/react waitfor to wait for a lazy loaded component before I compare snapshots.
The App.tsx is simply this:
const MyComponent = lazy(() => import('my/component'));
function App() {
return (
<Suspense
fallback={
<div>
<h1>Loading…</h1>
</div>
}
>
<MyComponent />
</Suspense>
);
MyComponent is:
export const MyComponent = memo(() => {
return <div data-testid='test' />;
});
And the test is:
describe('<App />', () => {
it('should render the App', async () => {
const { container, findByTestId } = render(<App />);
await waitFor(() => findByTestId('test'));
expect(container.firstChild).toMatchSnapshot();
});
});
The waitFor timeout because findByTestId('test') isn't finding the component. If I replace <MyComponent /> with <div data-testid='test' /> then the test works as expected.
Why doesn't render correctly if its being lazily loaded?
Adam
Solution 1:[1]
From the React.lazy doc:
React.lazytakes a function that must call a dynamicimport(). This must return aPromisewhich resolves to a module with adefaultexport containing a React component.
This is the signature of the React.lazy method:
function lazy<T extends ComponentType<any>>(
factory: () => Promise<{ default: T }>
): LazyExoticComponent<T>;
So the factory function should be:
const MyComponent = lazy(() => import('./component').then(({ MyComponent }) => ({ default: MyComponent })));
Besides, you don't need to use waitFor together with findBy* queries, because:
findBy*methods are a combination ofgetBy*queries andwaitFor
The complete working example:
component.tsx:
import React from 'react';
import { memo } from 'react';
export const MyComponent = memo(() => {
return <div data-testid="test" />;
});
index.tsx:
import React, { lazy, Suspense } from 'react';
const MyComponent = lazy(() => import('./component').then(({ MyComponent }) => ({ default: MyComponent })));
export function App() {
return (
<Suspense
fallback={
<div>
<h1>Loading…</h1>
</div>
}
>
<MyComponent />
</Suspense>
);
}
index.test.tsx:
import { render } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import React from 'react';
import { App } from '.';
describe('<App />', () => {
it('should render the App', async () => {
const { container, findByTestId } = render(<App />);
expect(container.firstChild).toMatchInlineSnapshot(`
<div>
<h1>
Loading…
</h1>
</div>
`);
const mycomp = await findByTestId('test');
expect(mycomp).toBeInTheDocument();
expect(container.firstChild).toMatchInlineSnapshot(`
<div
data-testid="test"
/>
`);
});
});
Test result:
PASS stackoverflow/71926928/index.test.tsx (11.382 s)
<App />
? should render the App (78 ms)
---------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
component.tsx | 100 | 100 | 100 | 100 |
index.tsx | 100 | 100 | 100 | 100 |
---------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 2 passed, 2 total
Time: 12.685 s
package versions:
"react": "^16.14.0",
"jest": "^26.6.3",
"@testing-library/react": "^11.2.7"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | slideshowp2 |
