Registry / testing / ra-test

ra-test

JSON →
library3.19.12jsnpmunverified

The `ra-test` package provides specialized utilities for efficiently testing applications built upon `react-admin`, a widely adopted frontend framework for developing administrative interfaces with React. As of early 2026, the current stable version of `react-admin` (and consequently `ra-test`, which is part of the same monorepo) is 5.14.6, characterized by a continuous release cadence with frequent minor updates and bug fixes. This library is specifically tailored to address the complexities of testing `react-admin` components, particularly custom views or logic that depend on the framework's intricate Redux store and React Router context. Unlike general React testing libraries, `ra-test` offers pre-configured wrappers like `TestContext` and helpers such as `renderWithRedux` that seamlessly provide the necessary `react-admin` environment, enabling developers to focus on the specific logic of their custom components rather than boilerplate setup. It differentiates itself by providing a robust, opinionated testing environment integrated deeply with `react-admin`'s architecture, making it easier to mock the `react-admin` state and dispatch actions for focused unit or integration tests within the ecosystem.

npm install ra-test
INSTALL
IMPORT
SIG · RA-TEST
R
ra-test
testingjavascriptv3.19.12
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TestContext
import { TestContext } from 'ra-test';
const { TestContext } = require('ra-test');
The primary component for wrapping react-admin components to provide necessary context for testing. ESM imports are standard.
renderWithRedux
import { renderWithRedux } from 'ra-test';
import renderWithRedux from 'ra-test';
A helper function that combines TestContext setup with @testing-library/react's render, exported as a named import.
defaultStore
import { defaultStore } from 'ra-test';
Provides a default Redux store configuration that can be used or extended for tests, useful with enableReducers.

This quickstart demonstrates how to use `TestContext` to wrap a custom `react-admin` component, providing the necessary Redux and routing contexts for unit testing with `@testing-library/react`. It shows how to pass default props and simulate user interactions.

import * as React from "react"; import { TestContext } from 'ra-test'; import { render, fireEvent, screen } from '@testing-library/react'; // Imagine this is your custom react-admin Edit view component const MyCustomEditView = ({ basePath, id, resource }) => { const handleClick = () => { // In a real app, this might dispatch an action or navigate console.log(`Action for resource ${resource} with ID ${id} at ${basePath}`); }; return ( <div data-testid="my-custom-edit-view"> <h1>Edit {resource} #{id}</h1> <p>Base Path: {basePath}</p> <button onClick={handleClick}>Perform Action</button> </div> ); }; describe('MyCustomEditView', () => { const defaultEditProps = { basePath: '/posts', id: '123', resource: 'posts', }; it('should render the custom edit view with provided props', () => { render( <TestContext enableReducers> <MyCustomEditView {...defaultEditProps} /> </TestContext> ); expect(screen.getByText('Edit posts #123')).toBeInTheDocument(); expect(screen.getByText('Base Path: /posts')).toBeInTheDocument(); }); it('should simulate a button click', () => { const consoleSpy = jest.spyOn(console, 'log'); render( <TestContext enableReducers> <MyCustomEditView {...defaultEditProps} /> </TestContext> ); fireEvent.click(screen.getByText('Perform Action')); expect(consoleSpy).toHaveBeenCalledWith('Action for resource posts with ID 123 at /posts'); consoleSpy.mockRestore(); }); });
Debug
Known issues
breakingThe `ra-test` package versions are tightly coupled with the major versions of `react-admin`. Using `ra-test@3.x.x` with `react-admin@4.x.x` or `5.x.x` will lead to incompatible APIs and runtime errors due to significant changes in `react-admin`'s internal structure and context providers.
fix
Ensure your `ra-test` package version matches the major version of your `react-admin` installation (e.g., `react-admin@5.x.x` requires `ra-test@5.x.x`). Upgrade both packages concurrently.
affects: <5.0.0
gotchaDirect unit testing of most `react-admin`'s built-in components is generally discouraged by the framework maintainers. `ra-test` is primarily for custom components that integrate with `react-admin`'s context, rather than testing react-admin's internals.
fix
Focus your testing efforts on end-to-end scenarios for core application flows and on unit/integration testing your *custom* React components, mocking `react-admin`'s data providers or using `TestContext` for dependencies.
affects: >=3.0.0
gotcha`react-admin` components heavily rely on React Context for Redux, React Router, and internal framework state. Without wrapping components under test in `TestContext` (or `renderWithRedux`), they will fail to initialize or function correctly.
fix
Always wrap your `react-admin` dependent components with `TestContext` or use the `renderWithRedux` helper. Remember to enable reducers if your component relies on Redux state changes (`<TestContext enableReducers />`).
affects: >=3.0.0
deprecatedThe use of `TestContext` without `enableReducers` will prevent Redux actions from being processed, making tests for components that dispatch actions ineffective. While not strictly deprecated, it's a common oversight.
fix
If your component dispatches Redux actions or relies on Redux state updates, always pass the `enableReducers` prop to `TestContext`: `<TestContext enableReducers>{...}</TestContext>`.
affects: >=3.0.0
Errors
Common errors & fixes
Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider>
`react-admin` components require a Redux store context, which `TestContext` provides, but this error indicates it's missing.
fix
Ensure the component under test is wrapped within `<TestContext>` or rendered using `renderWithRedux`. For example: `render(<TestContext><MyComponent /></TestContext>)`
Error: useLocation() may be used only in the context of a <Router> component.
`react-admin` components often use React Router hooks, requiring a Router context that `TestContext` provides.
fix
The component depending on `react-router` hooks or components must be rendered inside `TestContext`, which sets up the necessary `react-router` context. For example: `render(<TestContext><MyComponentThatUsesRouter /></TestContext>)`
TypeError: Cannot read properties of undefined (reading 'dispatch') or TypeError: Cannot read property 'dispatch' of undefined
Attempting to spy on `store.dispatch` or interact with the Redux store when `TestContext` has not yet exposed it or when `enableReducers` is not set.
fix
If spying on `dispatch`, ensure you use the render prop pattern for `TestContext` to access the `store` object: `<TestContext>{({ store }) => { dispatchSpy = jest.spyOn(store, 'dispatch'); return <MyComponent />; }}</TestContext>`. Also, ensure `enableReducers` is set if state changes are expected.
Upgrade
Version history
3.19.12latest on npm
Audit
Dependencies
reactrequiredCore dependency for all React components.
reduxrequiredUnderlying state management library for react-admin.
ra-corerequiredThe foundational react-admin package, providing core hooks and context.
react-domrequiredUsed for rendering React components into the DOM, essential for testing utilities like @testing-library/react.
react-reduxrequiredConnects React components to the Redux store provided by react-admin.
react-routerrequiredCore routing library, used internally by react-admin.
react-router-domrequiredDOM-specific bindings for React Router, used by react-admin for browser routing.
Agent activity
4 hits · last 30 days
node
4
Resources
ra-test — npm install ra-test · libregistry