Javascript Jest/Ezyme和样式化组件找不到模块
我在运行时遇到以下错误Javascript Jest/Ezyme和样式化组件找不到模块,javascript,reactjs,jestjs,enzyme,styled-components,Javascript,Reactjs,Jestjs,Enzyme,Styled Components,我在运行时遇到以下错误 npm run test src/components/documentEditor/levels/2_page/page.test.js ● Test suite failed to run Cannot find module 'react' from 'styled-components.cjs.js' However, Jest was able to find: './page.js' './page.t
npm run test
src/components/documentEditor/levels/2_page/page.test.js
● Test suite failed to run
Cannot find module 'react' from 'styled-components.cjs.js'
However, Jest was able to find:
'./page.js'
'./page.test.js'
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['web.js', 'js', 'web.ts', 'ts', 'web.tsx', 'tsx', 'json', 'web.jsx', 'jsx', 'node'].
See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string
{not find module'react'from'styled components.cjs.js}错误我认为是指正在测试的'Page'组件中的导入
如果我不运行任何测试,并且使用了styledComponents,那么“Page”组件可以完美运行
“页面”组件的标题如下所示(不是完整组件,只是前几行)
package.JSON中的Jest设置如下
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.19.0",
"enzyme-adapter-react-16": "^1.15.3",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-router-dom": "^5.0.1",
"react-scripts": "^3.4.3",
"react-transition-group": "^4.3.0",
"uuid": "^3.3.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"jest": {
"collectCoverageFrom": [
"src/components/documentEditor/*.js",
"!<rootDir>/node_modules/",
"!<rootDir>/src/index.js",
"!<rootDir>/src/registerServiceWorker.js"
]
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "http://localhost:5000"
}
修复
我导航到客户机目录,在那里安装并设置了样式(因为它们是安装在服务器目录中的,而当我从客户机目录运行jset时,它们没有被加载)
为什么这样做?应用程序是node(服务器)和react(客户端),这意味着有两个节点模块目录和两个package.json文件 当我运行应用程序时,我通常从服务器上运行它,服务器“并发”运行模块,然后运行客户端 react Beauty和样式化组件被安装到服务器的节点目录中,然后在我运行应用程序时加载它们 我从客户机目录运行Jest,这意味着没有加载服务器和节点模块
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.19.0",
"enzyme-adapter-react-16": "^1.15.3",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-router-dom": "^5.0.1",
"react-scripts": "^3.4.3",
"react-transition-group": "^4.3.0",
"uuid": "^3.3.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"jest": {
"collectCoverageFrom": [
"src/components/documentEditor/*.js",
"!<rootDir>/node_modules/",
"!<rootDir>/src/index.js",
"!<rootDir>/src/registerServiceWorker.js"
]
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "http://localhost:5000"
}
src/components/documentEditor/levels/2_page/page.test.js
● Test suite failed to run
Cannot find module 'react' from 'react-beautiful-dnd.cjs.js'
However, Jest was able to find:
'./page.js'
'./page.test.js'