[英]How to setup tsconfig paths in jest.config.js. Angular 8. jest-preset-angular
我在我的 tsconfig 中设置了绝对路径,可以在服务期间按预期工作,但不能开玩笑。
示例路径如下所示:
"paths": {
"@shared": "src/app/shared/index.ts"
}
然后在我可以使用的组件中
import { MySharedComponent, MyOtherSharedComponent } from '@shared';
我目前正在尝试转移到 Jest 进行测试。 在我的 jest.config.js 我有一个 moduleNameMapper 部分:
moduleNameMapper: {
'@shared/(.*)': 'src/app/shared/$1'
}
这导致
cannot find module @Shared
如果我将 tsconfig 路径更改为此:
"paths": {
"@shared/*": "src/app/shared/*"
}
这些不再起作用
import { MySharedComponent, MyOtherSharedComponent } from '@shared';
并且必须更新到这个
import { MySharedComponent } from '@shared/my-shared.component';
import { MyOtherSharedComponent } from '@shared/my-other-shared.component';
测试工作正常,项目运行正常,但这是一个大项目,我有数百个使用这种格式的导入
import { MySharedComponent, MyOtherSharedComponent } from '@shared';
有没有办法让 moduleNameMapper 使用这个路径
"@shared": "src/app/shared/index.ts"
您可以在tsconfig.json
中有多个 @shared paths
,一个用于@shared
, index.ts
用于子路径:
"paths": {
"@shared": "src/app/shared/index.ts",
"@shared/*": "src/app/shared/*"
}
对于 moduleNameMapper,您还可以添加第二个:
moduleNameMapper: {
'@shared/(.*)': 'src/app/shared/$1',
'@shared': 'src/app/shared/index.ts'
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.