Quick Setup
FlashList provides a Jest setup file that mocks the necessary measurements:Create jest-setup.js
Create a
jest-setup.js file in your project root (if you don’t have one already):jest-setup.js
What the Setup Does
The FlashList Jest setup mocks layout measurement functions to return fixed dimensions:Basic Test Example
Here’s a simple test using@testing-library/react-native:
Testing Common Scenarios
Testing Item Rendering
Testing Empty States
Testing User Interactions
Testing with Custom Mock Dimensions
If you need different mock dimensions for specific tests:Testing Different Layouts
Grid Layout
Masonry Layout
Testing with TypeScript
Fully typed test example:Best Practices
Use testID liberally
Add
testID props to make elements easy to query in tests:Test behavior, not implementation
Focus on what the user sees and does, not internal component details:
Mock external dependencies
Mock API calls, navigation, and other external dependencies:
Clean up after tests
Use
beforeEach and afterEach to reset state:Common Issues
Tests timing out or hanging
Tests timing out or hanging
This usually means the Jest setup file isn’t loaded. Verify:
jest-setup.jscontainsrequire("@shopify/flash-list/jestSetup")jest.config.jshassetupFiles: ['./jest-setup.js']- The path to jest-setup.js is correct
Items not rendering in tests
Items not rendering in tests
The mock dimensions might be too small. Try increasing them in a custom mock:
Can't find elements with getByText
Can't find elements with getByText
Make sure items are actually rendered. Check the mock viewport size or add
testID for more reliable queries.TypeScript errors with FlashList types
TypeScript errors with FlashList types
Import types explicitly:
Testing Libraries
Recommended testing libraries for FlashList:- React Native Testing Library
- Jest
- React Test Renderer
Complete Test Suite Example
Related
Testing Library Docs
Official React Native Testing Library documentation
Jest Documentation
Learn more about Jest testing framework