Warning: fopen(/www/wwwroot/www.wendadaohang.com/data/md5_content_title/ac/acdbe617684a9c26aa113e8947d2014b.txt): failed to open stream: No space left on device in /www/wwwroot/www.wendadaohang.com/inc/function.php on line 2468

Warning: flock() expects parameter 1 to be resource, bool given in /www/wwwroot/www.wendadaohang.com/inc/function.php on line 2469

Warning: fclose() expects parameter 1 to be resource, bool given in /www/wwwroot/www.wendadaohang.com/inc/function.php on line 2475
使用JEST遇到Cannot find module报错的问题解决 - 22问答网

使用JEST遇到Cannot find module报错的问题解决

如题所述

第1个回答  2022-06-13
使用JEST进行单元测试时出现报错:
Cannot find module 'src/index’ from 'index.js'
Resolver.resolveModule (node_modules/jest-resolve/build/index.js:276:11)

在 node_modules 有个包里面的 index.js 文件使用了 import { a } from 'src/index' ,报错意为这里找不到 'src/index' 文件地址。

最简单的就是把 import { a } from 'src/index' 改成 import { a } from './src/index'

但是如果是安装的包直接改源码不太好,换了环境进行单元测试都去改非常麻烦。所以继续查找更好的方法:

在 package.json 的 jest 配置项添加:

至此jest的这个报错问题得以解决!

如果小伙伴们还没解决自己的问题,可以再于jest官方issues搜索查阅:
https://github.com/facebook/jest/issues
https://github.com/jest-community/vscode-jest/issues/382

参考资料:
https://jestjs.io/docs/en/configuration#moduledirectories-arraystring
https://github.com/facebook/jest/issues/2458
https://stackoverflow.com/questions/50863312/jest-gives-cannot-find-module-when-importing-components-with-absolute-paths
相似回答