在对我的项目执行重构之后,我发现Jest找不到任何测试模块。它报告了以下错误。
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在研究了一段时间之后,我发现最小的可重现性示例如下:
node_modules/foo.js
function foo() {
return 'hello';
}
module.exports.foo = foo;node_modules/foo.test.js
test( 'foo' , ()=>{
console.error( require('./foo.js' ).foo() );
});如果foo.js和foo.test.js从node_modules目录中移出,Jest将正常工作。
为了避免长时间的相对包名,我将所有文件放在node_modules目录下。如果可能的话,我不想重新安置他们。
这一问题是否有任何解决办法,如果可能的话,是否有任何永久的适当解决办法?
(编辑)为什么要将这些放在node_modules中?
发布于 2022-09-01 05:54:51
这是一个已知的bug,该bug已经修复。
一个快速修复方法是在项目树的根目录中创建jest.config.js,如下所示:
const config = {
"testEnvironment": "node",
"testPathIgnorePatterns": [],
"haste": {
"retainAllFiles": true
}
};
module.exports = config; 也可以在package.json中添加以下内容:
"jest": {
"testEnvironment": "node",
"testPathIgnorePatterns": [],
"haste" : {
"retainAllFiles": true
}
} 这个答案是我的提醒,希望这能节省我朋友们宝贵的几个小时。
https://stackoverflow.com/questions/73564665
复制相似问题