Testing Strategy and Infrastructure
Relevant source files
The following files were used as context for generating this wiki page:
- README.md
- packages/react-devtools-inline/tests/e2e/components.test.js
- scripts/release/README.md
- packages/react-devtools/CONTRIBUTING.md
- fixtures/packaging/README.md
- fixtures/attribute-behavior/README.md
- packages/react-dom/src/tests/ReactRenderDocument-test.js
- packages/react-dom/src/tests/ReactDOMFizzServer-test.js
- packages/react-devtools-shared/src/hooks/tests/parseHookNames-test.js
- compiler/packages/babel-plugin-react-compiler/scripts/build-react-hooks-fixures.js
- compiler/apps/playground/tests/e2e/page.spec.ts
- scripts/devtools/build-and-test.js
- compiler/packages/react-mcp-server/src/index.ts
- packages/react-reconciler/src/tests/useSyncExternalStore-test.js
- fixtures/flight/scripts/build.js
The React project employs a comprehensive, multi-layered testing strategy to ensure the stability, correctness, and performance of the core library, compiler, and developer tools. This strategy integrates automated testing at various levels—unit, integration, and end-to-end—with a robust CI/CD pipeline and specialized tooling for performance analysis and manual verification. Development happens in the open, and community contributions for bug fixes and improvements are validated through this rigorous testing process.
The testing infrastructure is designed for gradual adoption and continuous delivery, with automated prereleases published on weekdays. This allows for rapid iteration and feedback on experimental features without affecting stable production users.
Sources: README.md:38-40, scripts/release/README.md:10-12, 21-23
Levels of Testing
React's testing approach can be visualized as a pyramid, with a broad base of unit and integration tests, followed by more focused E2E, fixture-based, and performance tests.
This diagram illustrates the different layers of testing within the React project.
Unit and Integration Testing
The foundation of React's testing strategy consists of unit and integration tests, primarily using the Jest framework. These tests cover individual functions, components, and hooks, as well as their interactions within the larger system.
- Framework: Jest
- Environment: A custom Jest environment,
ReactDOMServerIntegrationEnvironment, is used for server-side rendering tests. This environment sets up a JSDOM instance to simulate a browser environment. - Test Utilities: A suite of internal utilities, found in
internal-test-utilsandtest-utils/FizzTestUtils, provides helpers foract, assertions, and managing asynchronous operations. For example,ReactRenderDocument-test.jsandReactDOMFizzServer-test.jsmake extensive use ofactandassertLogfor testing component rendering and state updates. - Mocking: Mocks are used for dependencies like
schedulerandjest-fetch-mockto control asynchronous behavior and network requests in tests.
Sources: packages/react-dom/src/tests/ReactDOMFizzServer-test.js:10-13, 56-69, packages/react-dom/src/tests/ReactRenderDocument-test.js:25-32, packages/react-reconciler/src/tests/useSyncExternalStore-test.js:20-33, packages/react-devtools-shared/src/hooks/tests/parseHookNames-test.js:25-27
End-to-End (E2E) Testing
E2E tests validate user-facing features in a real browser environment. The React project uses Playwright for E2E testing of the React DevTools and the React Compiler Playground.
- Framework: Playwright (
@playwright/test) - Targets:
- React DevTools: Tests verify that DevTools can correctly inspect the component tree, view and edit props, parse hook names, and search for components.
- Compiler Playground: Tests ensure that the online playground correctly compiles user-provided code and reflects changes.
- Workflow: E2E tests typically navigate to a specific URL, interact with the page (e.g., clicking buttons, typing text), and assert that the UI updates as expected.
The following diagram shows a typical E2E test flow for React DevTools.
Fixture-Based and Manual Testing
For certain complex behaviors, React relies on fixture-based testing, which often requires manual verification. These fixtures are self-contained test cases designed to verify specific functionalities like packaging and attribute rendering.
- Packaging Fixtures: Located in
fixtures/packaging, these tests ensure that the built React distributions are usable in various environments (e.g., UMD, CommonJS). They are run by building React and the fixtures, starting a local server, and manually checking for rendering errors in a browser. - Attribute Behavior Fixtures: Found in
fixtures/attribute-behavior, these fixtures test how React handles various DOM attributes, including invalid or mis-capitalized ones. The test page displays a comparison between the behavior of the latest stable release and the locally built version.
Sources: fixtures/packaging/README.md:6-19, fixtures/attribute-behavior/README.md:7-16
Compiler Testing
The React Compiler is tested using a fixture-based approach. The test fixtures are automatically generated from the test cases defined in eslint-plugin-react-hooks. This ensures that the compiler's behavior is consistent with the Rules of Hooks.
The process is as follows:
- A script (
build-react-hooks-fixures.js) reads valid and invalid test cases fromeslint-plugin-react-hooks-test-cases. - For each test case, it runs the React Compiler twice: once with hook validation disabled and once with it enabled.
- Based on whether the code is expected to be valid and whether the compiler passes or fails, the script generates a fixture file with a specific prefix (e.g.,
error.invalid-,todo.error.invalid-). - These generated fixtures are then used as inputs for the compiler's own test suite.
Sources: compiler/packages/babel-plugin-react-compiler/scripts/build-react-hooks-fixures.js:28-76
Tooling and Infrastructure
Continuous Integration (CI)
React's CI pipeline, powered by GitHub Actions, is central to its development and release process. It automates building, testing, and publishing.
The diagram below outlines the CI workflow for releases.
Developer Tools Testing
Testing React DevTools involves a specialized setup to accommodate its nature as a browser extension and standalone application.
The general workflow is:
- Install Dependencies: Run
yarn installat the repository root. - Build/Download React: Contributors can either build React from source (
yarn build-for-devtools) or download a pre-built experimental version. - Run DevTools: Start the DevTools inline package in development mode (
yarn start). - Run Test Shell: Start the
react-devtools-shellpackage, which provides a test application. Changes to DevTools will hot-reload in the shell. - Run Unit Tests: Core DevTools functionality is covered by unit tests, which can be run with
yarn test-build-devtools.
Performance Testing
React includes a tool for measuring runtime performance to verify that changes improve, or at least do not regress, key metrics.
The review-react-runtime tool, defined in compiler/packages/react-mcp-server/src/index.ts, measures performance by running a given React component and collecting data.
Key Performance Metrics:
| Metric | Description | Goal |
|---|---|---|
| LCP | Largest Contentful Paint (loading speed) | ≤ 2.5s |
| INP | Interaction to Next Paint (input responsiveness) | ≤ 200ms |
| CLS | Cumulative Layout Shift (visual stability) | ≤ 0.10 |
| Actual Duration | Time spent rendering the committed update (React Profiler) | Varies |
| Base Duration | Time to render the entire subtree without memoization (React Profiler) | Varies |
The tool requires the input code to contain a standard App functional component and use prefixed React APIs (e.g., React.useState). It runs multiple iterations to calculate a mean for each metric.
Sources: compiler/packages/react-mcp-server/src/index.ts:311-352
Conclusion
The testing strategy for React is a robust and multi-faceted system that combines automated CI workflows, multiple levels of testing, and specialized tooling. From low-level unit tests in a mocked environment to full-blown E2E tests in a real browser, the infrastructure is designed to ensure the reliability of the core library, the compiler, and the developer tools. The inclusion of manual fixtures and dedicated performance analysis tools further strengthens the project's commitment to quality and performance, enabling both the core team and the community to contribute with confidence.