VS Code TypeScript Jest 模块映射
VS Code TypeScript Jest Module Mappings
我在 VS code 中将 TypeScript 与 Jest 一起使用,并且我映射了一些模块路径以便于访问这些模块。
非常简单,我的目录结构是这样的:
├── app
│ └── middleware
│ └── auth.ts
├── jest.config.js
├── package.json
├── package-lock.json
├── test
│ └── unittest
│ └── app
│ └── middleware
│ └── auth.test.ts
└── tsconfig.json
我的 tsconfig.json
:
中有这些映射
"baseUrl": ".",
"paths": {
"@app/*": [
"./app/*"
]
}
我的 jest.config.js
中的这些映射:
moduleNameMapper: {
"^@app/(.*)$": "<rootDir>/app/"
}
一切正常,我可以 运行 毫无问题地开玩笑,但 VS 代码无法识别文件 auth.test.ts
中的 @app
映射(在 auth.ts
中有效很好)。
有办法解决这个问题吗?
您可以使用包 module-alias 在运行时分配别名,然后利用 Jest 的 setupFile 选项确保它们始终在 Jest 运行时创建。
编辑:问题需要在对此答案的评论中得出的另一个解决方案
我在 VS code 中将 TypeScript 与 Jest 一起使用,并且我映射了一些模块路径以便于访问这些模块。
非常简单,我的目录结构是这样的:
├── app
│ └── middleware
│ └── auth.ts
├── jest.config.js
├── package.json
├── package-lock.json
├── test
│ └── unittest
│ └── app
│ └── middleware
│ └── auth.test.ts
└── tsconfig.json
我的 tsconfig.json
:
"baseUrl": ".",
"paths": {
"@app/*": [
"./app/*"
]
}
我的 jest.config.js
中的这些映射:
moduleNameMapper: {
"^@app/(.*)$": "<rootDir>/app/"
}
一切正常,我可以 运行 毫无问题地开玩笑,但 VS 代码无法识别文件 auth.test.ts
中的 @app
映射(在 auth.ts
中有效很好)。
有办法解决这个问题吗?
您可以使用包 module-alias 在运行时分配别名,然后利用 Jest 的 setupFile 选项确保它们始终在 Jest 运行时创建。
编辑:问题需要在对此答案的评论中得出的另一个解决方案