React Shallow Renderer provides a utility for unit testing React components by rendering them "one level deep." This means it asserts facts about a component's `render` method output without instantiating or rendering child components, and crucially, without requiring a DOM environment. The current stable version is 16.15.0, with the last significant update in October 2020. While historically useful for isolating component logic, React's official documentation for React 19 explicitly recommends against shallow rendering, urging developers to migrate to `@testing-library/react` or `@testing-library/react-native` for more robust and maintainable tests that focus on user behavior rather than implementation details. The package has not seen active development recently, making it a lower-level alternative compared to libraries like Enzyme, which offers a more feature-rich API over similar shallow rendering functionality.
npm install react-shallow-rendererVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic shallow rendering of a component and accessing its top-level output and props for testing.
Consider rewriting tests using `@testing-library/react` or `@testing-library/react-native` which focus on testing components from a user's perspective, without relying on implementation details.
Update all imports from `const ShallowRenderer = require('react-shallow-renderer');` to `import ShallowRenderer from 'react-shallow-renderer';`.Ensure your bundler (e.g., Webpack, Rollup) is configured to correctly resolve ES modules from `package.json`'s `main` field or implicit ES module resolution rules.
If testing components that rely on `refs` (e.g., for direct DOM manipulation or imperative calls), shallow rendering is not suitable. Consider using `mount` from Enzyme or a full DOM rendering solution like React Testing Library with `@testing-library/react` which provides utilities for interacting with the rendered DOM.
Switch to ES module import syntax: `import ShallowRenderer from 'react-shallow-renderer';`
Shallow rendering is incompatible with `refs`. If your test requires interacting with `refs`, you must use a full DOM rendering solution (e.g., Enzyme's `mount` or React Testing Library) that simulates a browser environment. Alternatively, refactor your component to avoid direct ref usage in the logic being tested.