简体   繁体   English

如何在 jest.config.js 中设置 tsconfig 路径。 Angular 8. 开玩笑预设角度

[英]How to setup tsconfig paths in jest.config.js. Angular 8. jest-preset-angular

I have absolute paths set up in my tsconfig that work as expected during serve however do not work for jest.我在我的 tsconfig 中设置了绝对路径,可以在服务期间按预期工作,但不能开玩笑。

An example path looks like this:示例路径如下所示:

"paths": {
    "@shared": "src/app/shared/index.ts"
}

Then in a component I can use然后在我可以使用的组件中

import { MySharedComponent, MyOtherSharedComponent } from '@shared';

I am currently trying to move to Jest for testing.我目前正在尝试转移到 Jest 进行测试。 In my jest.config.js I have a moduleNameMapper section:在我的 jest.config.js 我有一个 moduleNameMapper 部分:

moduleNameMapper: {
    '@shared/(.*)': 'src/app/shared/$1'
}

This results in这导致

cannot find module @Shared

If I change my tsconfig path to this:如果我将 tsconfig 路径更改为此:

"paths": {
    "@shared/*": "src/app/shared/*"
}

these no longer work这些不再起作用

import { MySharedComponent, MyOtherSharedComponent } from '@shared';

and have to be updated to this并且必须更新到这个

import { MySharedComponent } from '@shared/my-shared.component';
import { MyOtherSharedComponent } from '@shared/my-other-shared.component';

the tests work fine and the project runs ok however it is a large project and I have hundreds of imports that use this format测试工作正常,项目运行正常,但这是一个大项目,我有数百个使用这种格式的导入

import { MySharedComponent, MyOtherSharedComponent } from '@shared';

Is there a way to get moduleNameMapper to work with this path有没有办法让 moduleNameMapper 使用这个路径

"@shared": "src/app/shared/index.ts"

You can have several paths for @shared in tsconfig.json , one for the index.ts and others for sub-paths:您可以在tsconfig.json中有多个 @shared paths ,一个用于@sharedindex.ts用于子路径:

"paths": {
    "@shared": "src/app/shared/index.ts",
    "@shared/*": "src/app/shared/*"
}

For the moduleNameMapper you can also add a second one:对于 moduleNameMapper,您还可以添加第二个:

moduleNameMapper: {
    '@shared/(.*)': 'src/app/shared/$1',
    '@shared': 'src/app/shared/index.ts'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 angular 工作区配置 `jest-preset-angular` - Configure `jest-preset-angular` with angular workspace 相当于 jest-preset-angular 中的 and.callThrough() - Equivalent of and.callThrough() in jest-preset-angular Angular 8 和 jest - 找不到文件:jest-preset-angular/InlineHtmlStripStylesTransformer.js - Angular 8 and jest - File not found: jest-preset-angular/InlineHtmlStripStylesTransformer.js 运行jest-preset-angleular时ENONET错误 - ENONET error while running jest-preset-angular 模块<rootDir>未找到转换选项中的 /node_modules/jest-preset-angular/preprocessor.js - Module <rootDir>/node_modules/jest-preset-angular/preprocessor.js in the transform option was not found Angular 4 + Jest-Preset-Angular:使用从文件导入的触发器在主机上测试动画 - Angular 4 + Jest-Preset-Angular : testing animation on host with trigger imported from file 如何在Stackblitz中使用角度设置笑话 - How to setup jest with angular in stackblitz Angular 8 与 Jest 使用路径 - Angular 8 with Jest using paths 如何使用 jest js 在 Angular 中 createSpyObj? - How to createSpyObj in Angular using jest js? Angular CLI 8.在 tsconfig.json 中使用“路径”时“ng serve”失败 - Angular CLI 8.“ng serve” fails when using “paths” in tsconfig.json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM