替换fireEvent为userEvent后出现@testing-library/dom模块找不到报错
解决@testing-library/user-event依赖缺失问题
问题原因
@testing-library/user-event 核心依赖 @testing-library/dom 模块,但你的项目中未安装该依赖,或是版本不匹配导致无法被正确识别。
解决方案步骤
1. 安装缺失的依赖
直接安装@testing-library/dom开发依赖:
npm install --save-dev @testing-library/dom # 或使用yarn yarn add --dev @testing-library/dom
2. 校验版本兼容性
若安装后仍报错,大概率是user-event与@testing-library/react版本不兼容:
user-eventv14+ 要求@testing-library/react版本≥12.1.0- 可直接升级到最新兼容版本:
# npm npm install --save-dev @testing-library/react@latest @testing-library/user-event@latest # yarn yarn add --dev @testing-library/react@latest @testing-library/user-event@latest
3. 清除缓存重装依赖
如果上述操作无效,尝试清除包管理工具缓存并重新安装所有依赖:
# npm 操作 npm cache clean --force rm -rf node_modules package-lock.json npm install # yarn 操作 yarn cache clean rm -rf node_modules yarn.lock yarn install
验证修复
重新运行测试套件确认问题解决:
npm test # 或 yarn test
内容的提问来源于stack exchange,提问作者paraS elixiR
相关产品推荐
相关产品推荐

