[英]How to make jest recognize imports relative to project root directory?
Is it possible to make Jest work requiring modules relative to the root project directory?是否可以让 Jest 工作需要相对于根项目目录的模块?
For example, here's my project structure:例如,这是我的项目结构:
If I'm on notes.test.js and want to import notes.js, I need to do the following:如果我在 notes.test.js 并且想要导入 notes.js,我需要执行以下操作:
const notes = require('../../notes.js');
Is it possible to configure Jest, so the line below would work from notes.test.js?是否可以配置 Jest,所以下面的行可以从 notes.test.js 工作?
const notes = require('notes.js');
I'd like to have that, so I could better understand what's being imported.我想要那个,所以我可以更好地了解正在导入的内容。 It's okay to figure this out on a small project like that, but I'm wondering how complicated it would be on large projects.
在这样的小项目上解决这个问题是可以的,但我想知道在大型项目上会有多复杂。 I tried to look for an answer on my own but couldn't figure it out.
我试图自己寻找答案,但无法弄清楚。
After trying a little more, it seems adding this to my package.json did the trick:在尝试了更多之后,似乎将它添加到我的 package.json 就可以了:
"jest": {
"moduleFileExtensions": ["js"],
"moduleDirectories": ["node_modules", "bower_components", "."]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.