调用Enzyme mount函数报错:已配置setupTests仍提示未配置适配器怎么办?
I’ve run into this exact frustrating issue before—you’ve got your Enzyme adapter properly set up in setupTests.js, confirmed Jest is loading the file (thanks to that console.log(1) working), but you still get the adapter missing error. And weirdly, copying the config code directly into each test file fixes it. Let’s break down the likely causes and fixes:
First, Verify Version Compatibility
Since you’re using React 16, make sure your Enzyme and adapter versions are fully compatible. Enzyme 3.x requires specific adapter versions for different React 16 sub-versions:
- React 16.0-16.2: Use
enzyme-adapter-react-16.0 - React 16.3+: Use
enzyme-adapter-react-16.3(or the broaderenzyme-adapter-react-16if you’re on a newer React 16 patch)
If you’re unsure, reinstall both packages to match:
npm uninstall enzyme enzyme-adapter-react-16 npm install --save-dev enzyme enzyme-adapter-react-16.3
Fix the Jest Setup Timing (Most Common Fix)
The biggest culprit here is usually the difference between Jest’s setupFiles and setupFilesAfterEnv options.
Your current config uses setupFiles, which runs before Jest’s testing environment (like jsdom) is fully initialized. Enzyme’s configuration needs to run after the DOM environment is ready to be properly applied across all test files.
Update your package.json Jest config to use setupFilesAfterEnv instead:
"jest": { "testEnvironment": "jest-environment-jsdom-global", "setupFilesAfterEnv": [ "<rootDir>/jestConfigs/setupTests.js" ], "moduleNameMapper": { "^.+\\.(css|scss)$": "<rootDir>/jestConfigs/CSSStub.js", "^.+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/jestConfigs/fileStub.js", "/common-mms/(.+)": "<rootDir>/App/common-mms/$1" } }
This ensures your Enzyme adapter is configured after Jest has set up the full testing environment, so the config is picked up by all test files.
Clear Cached Files
Sometimes old cached modules or Jest state can cause unexpected behavior. Clear them with these commands:
npx jest --clearCache rm -rf node_modules/.cache
Then re-run your tests to see if the error goes away.
Double-Check Test File Import Order
While less likely, make sure none of your test files are importing Enzyme methods (like mount) or React components before Jest has a chance to run your setup file. Even though setupFiles/setupFilesAfterEnv run first, a rare edge case with module hoisting could cause this—though switching to setupFilesAfterEnv usually eliminates this risk.
To recap your original setup for context:
- Your
setupTests.jsis correctly configured for React 16:import { configure } from 'enzyme'; import Adapter from 'enzyme-adapter-react-16'; configure({ adapter: new Adapter() }); - You confirmed Jest loads this file (the
console.log(1)prints), but the error persists until you copy the config into individual test files.
The setupFilesAfterEnv change is almost always the fix for this scenario. Give it a try first!
内容的提问来源于stack exchange,提问作者kurumkan

