REACT Jest-测试套件无法运行

时间:2020-02-16 08:57:59

标签: reactjs jestjs enzyme

我创建了样例react项目并添加了JEST和酶依赖性来测试我的组件。但是在运行测试套件时出现以下错误: 错误: 无法从“ App.js”中找到模块“组件/添加”

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)

测试套件:1个失败,总共1个 测试:总计0 快照:共0个 时间:4.035秒 运行所有测试套件。

任何人都可以帮助解决此问题,我们将乐意分享更多详细信息。

2 个答案:

答案 0 :(得分:0)

您是否尝试过使用相对导入?

select product.id, name, hand, count(qty) 
from product 
left join stock on pid=product.id

让我知道是否有帮助:)

答案 1 :(得分:0)

导入的编写方式,您可能在路径中使用别名。如果您使用的是webpack,请检查webpack.config.json中的resolve.alias字段以确认这一点。

如果确实设置了别名,则还必须将它们添加到jest配置中。这是使用moduleNameMapper完成的。例如,

{
  "moduleNameMapper": {
    "components/(.*)$": "<rootDir>/PATH_TO_COMPONENTS/$1",
  }

}