You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Enzyme mount函数报错:已配置setupTests仍提示未配置适配器怎么办?

Fixing "Enzyme expects an adapter to be configured" Error Even When setupTests.js Is Loaded

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 broader enzyme-adapter-react-16 if 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.js is 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:27:14