简体   繁体   English

当测试位于“node_modules”目录下时,Jest 找不到测试

[英]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.jsfoo.test.jsnode_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 作为绝对导入的官方解决方案

This is a known bug and the bug was already fixed.这是一个已知的错误,并且该错误已被修复。

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.

相关问题 运行测试时无法从“node_modules/react-native/jest/setup.js”中找到模块“@babel/runtime/helpers/interopRequireDefault” - Cannot find module '@babel/runtime/helpers/interopRequireDefault' from 'node_modules/react-native/jest/setup.js' when I run tests 尝试运行测试时 Karma/systemjs 没有在 node_modules 目录中查找 - Karma/systemjs not looking in node_modules directory when trying to run tests GitLab CI 找不到模块错误(当它在 node_modules 中时) - GitLab CI cannot find module error (when it is in node_modules) 赛普拉斯测试无法识别 node_modules 包类型 - Cypress tests not recognizing node_modules package types 运行节点脚本时出错,找不到模块'mongodb / node_modules / bson' - Error when running node script , Cannot find module 'mongodb/node_modules/bson' 无法加载我手动放在/ node_modules /目录中的模块 - Cannot load module I put manually in /node_modules/ directory 不同目录中的node_modules - node_modules in a different directory Jest:如何模拟 node_modules 中的库? - Jest: How to mock a library in node_modules? 打字稿无法在node_modules文件夹的定义文件中找到类型 - Typescript cannot find type in definition file in node_modules folder 找不到模块'mongodb / node_modules / bson' - Cannot find module 'mongodb/node_modules/bson'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM