Reactjs REACT Jest-测试套件无法运行
我已经创建了样例react项目,并添加了JEST和酶依赖项来测试我的组件。但运行测试套件时出现以下错误: 错误: 无法从“App.js”中找到模块“components/Add”Reactjs REACT Jest-测试套件无法运行,reactjs,jestjs,enzyme,Reactjs,Jestjs,Enzyme,我已经创建了样例react项目,并添加了JEST和酶依赖项来测试我的组件。但运行测试套件时出现以下错误: 错误: 无法从“App.js”中找到模块“components/Add” However, Jest was able to find: 'components/Add.js' You might want to include a file extension in your import, or update your 'moduleFileExtensions', which
However, Jest was able to find:
'components/Add.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
However, Jest was able to find:
'./App.css'
'./App.js'
'./App.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
1 | import React from 'react';
> 2 | import Add from 'components/Add';
| ^
3 |
4 |
5 | function App() {
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
at Object.<anonymous> (src/App.js:2:1)
然而,Jest能够找到:
'components/Add.js'
您可能希望在导入中包含文件扩展名,或更新“moduleFileExtensions”,当前为['web.js','js','web.ts','ts','web.tsx','tsx','json','web.jsx','jsx','node']。
看见https://jestjs.io/docs/en/configuration#modulefileextensions-数组字符串
然而,Jest发现:
“./App.css”
“./App.js”
“./App.test.js”
您可能希望在导入中包含文件扩展名,或更新“moduleFileExtensions”,当前为['web.js','js','web.ts','ts','web.tsx','tsx','json','web.jsx','jsx','node']。
看见https://jestjs.io/docs/en/configuration#modulefileextensions-数组字符串
1 |从“React”导入React;
>2 |从“组件/添加”导入添加;
| ^
3 |
4 |
5 |功能应用程序(){
在Resolver.resolveModule(node_modules/jest resolve/build/index.js:259:17)
at对象。(src/App.js:2:1)
测试套件:1个失败,共1个
测试:共0项
快照:共0个
时间:4.035s
运行所有测试套件
任何人都可以帮助解决此问题,并乐意分享更多详细信息。您是否尝试使用相对导入
import Add from './components/Add';
如果有帮助,请告诉我:)导入的编写方式可能是使用别名作为路径。如果使用webpack,请检查webpack.config.json中的
resolve.alias
字段以确认这一点
如果确实设置了别名,则还必须将其添加到jest配置中。这是通过使用完成的。例如
{
"moduleNameMapper": {
"components/(.*)$": "<rootDir>/PATH_TO_COMPONENTS/$1",
}
{
“moduleNameMapper”:{
“组件/(.*)$”:“/PATH_TO_components/$1”,
}
}这是因为Jest知道“组件”在哪里,请查看此信息并在您的Jest.config.js中进行配置,谢谢您的回复。是的,我正在网页包配置中使用别名。我将按照您的建议在Jest配置中添加上述配置,然后让您知道它是否工作。使用上述配置,原始错误已解决,但面临以下错误,找不到解决方案: