Empty list when testing with react-testing-library
See original GitHub issueHi, first of all, congrats for your awesome library! I’ve been trying to test some components where I use it, but the list never gets rendered 😞
import * as React from 'react';
import { Virtuoso } from 'react-virtuoso';
import { render } from '@testing-library/react';
function Component () {
return (
<Virtuoso
totalCount={100}
item={index => <div>{index}</div>}
/>
);
}
describe('Test', () => {
it('should render list of items', () => {
const { debug } = render(<Component />);
debug(); // Prints an empty list
});
});
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:22 (6 by maintainers)
Top Results From Across the Web
how to test a component if the component returns empty array ...
i am trying to learn react-testing-library and i got stuck at this point. I have one array that returns an empty array in...
Read more >API | Testing Library
React Testing Library re-exports everything from DOM Testing Library as well. ... See Queries for a complete list. Example.
Read more >React JS Testing Lists (TDD) | React Testing Library - YouTube
In this video I show you how to test lists in React.js! We'll take a test driven development (TDD) approach to first focus...
Read more >Testing Recipes - React
Common testing patterns for React components. Note: This page assumes you're using Jest as a test runner. If you use a different test...
Read more >React Integration Testing with React-testing-library - Toptal
This tutorial explains how to properly employ react-testing-library to conduct integration tests on your React app.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

The two provided solutions didn’t seemed great to me,
Setting the
initialItemCountfixed in the code when not in SSR is quite dangerous, as it’ll basically render all elements thus removing the purpose of Virtualization and also can lead to multiple renders.The second solution to override the
initialItemCounton the component code that uses Virtuoso according to the environment that’s running (test vs production) doesn’t seem great either as the component code will be messy.A better solution in my opinion is to override the Virtuoso implementation by creating a mocked version with a HOC that set this attribute, like this: (in this example I’m setting the
initialItemCountto the same value astotalCountso it render all elements)Hi,
I am not familiar with the testing-library internals, but my guess is that it is executed in a node environment, bypassing the hooks, effects, etc. If this is so, you can probably make it work by setting the
initialItemCountprop, available in v0.10 (released yesterday). Check the demo: https://stackblitz.com/edit/react-virtuoso-server-side-rendering?file=example.js It is also mentioned in the API reference: https://virtuoso.dev/virtuoso-api-reference/Please let me know if this works for you since it is a new feature.