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