在运行npm test
时,我得到了以下错误:
Cannot find module 'react-dom/client' from 'node_modules/@testing-library/react/dist/pure.js'
Required stack:
node_modules/@testing-library/react/dist/pure.js
node_modules/@testing-library/react/dist/index.js
所有必要的软件包似乎都安装好了。我重新安装了react-dom
,但没有帮助。下面提供在我的测试文件中使用的导入:
import React from "react";
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';
另外提供我的package.json
{
"name": "fe",
"version": "0.1.0",
"private": true,
"dependencies": {
"@fontsource/roboto": "^4.5.3",
"@material-ui/core": "^4.12.3",
"@material-ui/icons": "^4.11.2",
"@mui/icons-material": "^5.5.0",
"@mui/material": "5.5.3",
"@mui/styles": "^5.5.1",
"@reduxjs/toolkit": "^1.8.0",
"@testing-library/jest-dom": "5.16.3",
"@testing-library/react": "13.0.0",
"@testing-library/user-event": "14.0.4",
"axios": "^0.26.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-hook-form": "^7.28.1",
"react-redux": "^7.2.6",
"react-router-dom": "^6.2.2",
"react-scripts": "5.0.0",
"redux": "^4.1.2",
"styled-components": "^5.3.5",
"web-vitals": "^2.1.4",
"yup": "^0.32.11"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@types/jest": "^27.4.0",
"@types/node": "^16.11.25",
"@types/react": "^17.0.39",
"@types/react-dom": "^17.0.11",
"@types/styled-components": "^5.1.24",
"@typescript-eslint/eslint-plugin": "^5.12.0",
"@typescript-eslint/parser": "^5.12.0",
"eslint": "^8.9.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^8.4.0",
"eslint-import-resolver-typescript": "^2.5.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.28.0",
"eslint-plugin-react-hooks": "^4.3.0",
"prettier": "2.5.1",
"typescript": "^4.5.5"
}
}
发布于 2022-04-02 09:43:20
我认为这是因为您的@/react使用更新的版本,只需使用12.1.2版本进行测试。
发布于 2022-05-10 09:07:13
根据@ notes库/react发布说明的说法,version 13
(及以上)已经放弃了对React 17
和bellow的支持。因此,在React <= 17
中使用这个库会导致这个错误(或者其他错误)。
您可以降级到任何版本的@testing-library/react
贝娄13来修复它。在撰写本文时,最新的版本将是12.1.5
,它可以很好地与React 17
协同工作。
另一种选择是升级到18岁及以上。
发布于 2022-05-04 14:33:56
我还得到了一个错误“无法找到模块‘react/client’”,而没有提到@testing library/react。
看起来,连接React redux提供程序的语法在这里发生了变化:https://react-redux.js.org/introduction/getting-started (在代码块中,它提到了"// mentions 18")。
为了使这个工作,我必须确保我是在版本18或以上的反应和反应-多玛,并更新了所有其他客户端npm包。许多项目需要更新,包括:
npm update package-name --legacy-peer-deps
我花了几个小时才弄明白。希望它能帮到别人!
https://stackoverflow.com/questions/71713405
复制相似问题