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

React项目Jest测试遇babel-jest版本冲突,能否指定依赖树特定版本?

React前端Jest测试依赖冲突问题

我在主项目中创建了一个用于React前端开发的子文件夹,开发代码参考自React Bootstrap官方文档。尝试使用Jest测试前端时出现如下错误:

$ npm test -- App.test.js

> reactfrontend@0.1.0 test
> react-scripts test App.test.js


There might be a problem with the project dependency tree.
It is likely not a bug in Create React App, but something you need to fix locally.

The react-scripts package provided by Create React App requires a dependency:

  "babel-jest": "23.6.0"

Don't try to install it manually: your package manager does it automatically.
However, a different version of babel-jest was detected higher up in the tree:

  C:\Users\manuchadha\OneDrive - Microsoft\Manu_Personal\leren\views\reactfrontend\node_modules\babel-jest (version: 27.5.1)

Manually installing incompatible versions is known to cause hard-to-debug issues.

If you would prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That will permanently disable this message but you might encounter other issues.

To fix the dependency tree, try following the steps below in the exact order:

  1. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
  2. Delete node_modules in your project folder.
  3. Remove "babel-jest" from dependencies and/or devDependencies in the package.json file in your project folder.
  4. Run npm install or yarn, depending on the package manager you use.

In most cases, this should be enough to fix the problem.
If this has not helped, there are a few other things you can try:

  5. If you used npm, install yarn and repeat the above steps with it instead.
     This may help because npm has known issues with package hoisting which may get resolved in future versions.

  6. Check if C:\Users\manuchadha\OneDrive - Microsoft\Manu_Personal\leren\views\reactfrontend\node_modules\babel-jest is outside your project directory.
     For example, you might have accidentally installed something in your home folder.

  7. Try running npm ls babel-jest in your project folder.
     This will tell you which other package (apart from the expected react-scripts) installed babel-jest.

If nothing else helps, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That would permanently disable this preflight check in case you want to proceed anyway.

P.S. We know this message is long but please read the steps above :-) We hope you find them helpful!

执行npm ls babel-jest后,发现存在两个版本的babel-jest:

├─┬ jest@27.5.1
│ └─┬ @jest/core@27.5.1
│   └─┬ jest-config@27.5.1
│     └── babel-jest@27.5.1
└─┬ react-scripts@2.1.8
  ├── babel-jest@23.6.0
  └─┬ jest@23.6.0
    └─┬ jest-cli@23.6.0
      └─┬ jest-config@23.6.0
        └── babel-jest@23.6.0 deduped

我已尝试按照提示步骤修复,但仍持续报错,请问能否从node_modules依赖树中指定使用react-scripts所需的特定版本的babel-jest?


内容的提问来源于stack exchange,提问作者Manu Chadha

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 12:18:13