[英]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
,一个用于@shared
, index.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.