React/Jest/Enzyme中wrapper.state()与instance.state()均为null的问题
wrapper.state() and wrapper.instance().state() Returning Null in React 16 + Enzyme Tests Hey there, I feel your pain—spending hours stuck on a test issue that seems like it should just work is the worst. Let’s walk through the most common reasons this happens with your React 16, Jest, Enzyme, Webpack 4, and Babel stack, and how to fix them:
Enzyme Adapter Mismatch
React 16 requires theenzyme-adapter-react-16package—older adapters won’t play nice. If your adapter isn’t set up correctly, Enzyme can’t properly interact with your component’s state.
Fix steps:- Install the adapter:
npm install --save-dev enzyme-adapter-react-16 - Add a setup file (like
src/setupTests.js) with:import { configure } from 'enzyme'; import Adapter from 'enzyme-adapter-react-16'; configure({ adapter: new Adapter() }); - Verify Jest picks up this file—check your
jest.config.jsorpackage.jsonJest settings forsetupFilesAfterEnvpointing to the setup file.
- Install the adapter:
Testing a Functional Component
Heads up:wrapper.state()andwrapper.instance().state()only work for class components. Functional components use Hooks likeuseStateand don’t expose state via these methods.
Fix: Test functional components by asserting on element behavior (usingwrapper.find()) instead of trying to access state directly. You can also use Jest spies to mock Hooks or validate component outputs based on props.Component Not Fully Mounted
Shallow rendering (shallow()) might not trigger all lifecycle methods needed to initialize state, especially if your component sets state in methods likecomponentDidMount.
Fix: Try usingmount()instead ofshallow()to render the full component tree. Note thatmount()requires a DOM environment—Jest usually includesjsdomby default, but double-check your config to be sure.Babel Transpilation Issues
If Babel isn’t processing your React components correctly (like class properties or lifecycle methods), Enzyme can’t access the component instance properly.
Fix:- Ensure
@babel/preset-reactis installed and configured in your.babelrcorbabel.config.js:{ "presets": ["@babel/preset-react", "@babel/preset-env"] } - If you use class properties, install and add
@babel/plugin-proposal-class-propertiesto your Babel plugins.
- Ensure
State is Actually Initialized to Null
It sounds obvious, but double-check your component’s initial state setup. Maybe you have a typo in a state property name, or an async operation that hasn’t completed when you try to access state.
Fix: Addconsole.log(this.state)in your component’scomponentDidMount(oruseEffectfor functional components) to confirm state is set correctly. For async state changes, useasync/awaitwithwrapper.update()in your test to wait for updates.Outdated Enzyme Version
Older Enzyme versions might have compatibility bugs with React 16 that break state access.
Fix: Update Enzyme and its adapter:npm update enzyme enzyme-adapter-react-16
If none of these fix your issue, sharing a minimal reproducible example of your component and test code would help narrow things down further. Happy debugging!
内容的提问来源于stack exchange,提问作者Jeremy Little

