简体   繁体   English

如何让 jest 识别相对于项目根目录的导入?

[英]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.

相关问题 在 create-react-library 库中配置相对于根目录的导入 - Configure imports relative to root directory in create-react-library library Angular承认从root进口? - Angular recognize imports from root? 如何指定相对于项目根目录的路径? - how to specify a path relative to the project root? 如何使动态导入与 Jest 一起使用? - How do I make dynamic imports work with Jest? 如何用Jest测试进出口? - How to test imports/exports with Jest? JEST 和 ES6 导入 - 基于根文件夹的导入不起作用 - JEST and ES6 import - root folder based imports does not working 未找到模块:您尝试导入项目 src/ 目录之外的模块。 不支持 src/ 之外的相对导入 - Module not found: You attempted to import which falls outside of the project src/ directory. Relative imports outside of src/ are not supported 未找到模块错误:您尝试导入项目 src/ 目录之外的内容。 不支持 src/ 之外的相对导入 - Module not found Error: You attempted to import which falls outside of the project src/ directory. Relative imports outside of src/ are not supported 如何修复 NestJS 项目结构中导入的相对路径? - How can I fix the relative path of imports inside NestJS project structure? 如何使 ts-jest 使用在我的导入中导入的 js 文件的导入/导出语法? - How to make ts-jest work with import/export syntax of the js files that are being imported in my imports?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM