[英]Jest cannot find tests when tests are located under `node_modules` directory
After I performed refactoring on my project, I found that Jest could not find any test modules.在我对我的项目进行重构之后,我发现 Jest 找不到任何测试模块。 It reported following error.
它报告了以下错误。
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
No files found in /home/.../node_modules/.../... /api.
Make sure Jest's configuration does not exclude this directory.
To set up Jest, make sure a package.json file exists.
Jest Documentation: https://jestjs.io/docs/configuration
Pattern: - 0 matches
After I examined a while, I found the smallest reproducible example is following:在我检查了一段时间后,我发现最小的可重现示例如下:
node_modules/foo.js node_modules/foo.js
function foo() {
return 'hello';
}
module.exports.foo = foo;
node_modules/foo.test.js node_modules/foo.test.js
test( 'foo' , ()=>{
console.error( require('./foo.js' ).foo() );
});
And if foo.js
and foo.test.js
are moved out from node_modules
directory, Jest works as normal.如果
foo.js
和foo.test.js
从node_modules
目录中移出,Jest 将正常工作。
In order to avoid long relative package names, I put all files under node_modules
directory.为了避免长的相对 package 名称,我将所有文件放在
node_modules
目录下。 If possible, I don't want to relocate them.如果可能的话,我不想搬迁他们。
Is there any workaround or, if possible, any permanent proper solution for the issue?是否有任何解决方法,或者如果可能的话,是否有针对该问题的永久适当解决方案?
Edited) Why do you want to put those in node_modules
?已编辑)为什么要将它们放在
node_modules
中?
See Document src/node_modules as official solution for absolute imports请参阅文档 src/node_modules 作为绝对导入的官方解决方案
A quick fix is creating jest.config.js
in the root directory of your project tree as following:快速修复是在项目树的根目录中创建
jest.config.js
,如下所示:
const config = {
"testEnvironment": "node",
"testPathIgnorePatterns": [],
"haste": {
"retainAllFiles": true
}
};
module.exports = config;
Or you can add following in the package.json
:或者您可以在
package.json
中添加以下内容:
"jest": {
"testEnvironment": "node",
"testPathIgnorePatterns": [],
"haste" : {
"retainAllFiles": true
}
}
This answer is a sort of my reminder;这个答案是我的一种提醒; hoping this saves a couple of precious hours of my friends.
希望这可以节省我朋友的几个宝贵时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.