[英]ts-jest: map '@' char to /src folder
I have a node project with typescript, and I have the following paths configured in my tsconfig.json file:我有一个带有打字稿的节点项目,并且在我的 tsconfig.json 文件中配置了以下路径:
"paths": { /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
"@project/*": ["./../*"],
"@*": ["./*"],
},
My config files are at /project folder, and my sources at /project/src我的配置文件位于 /project 文件夹中,而我的源位于 /project/src
It works ok, and it correctly maps @xxx/yyy to src/xxx/yyy and @project/package.json to /project/package.json它工作正常,它正确地将 @xxx/yyy 映射到 src/xxx/yyy 和 @project/package.json 到 /project/package.json
I'm trying to achieve the same with ts-jest, I tried with the folowing moduleNameMapper in my jest.config.js file:我正在尝试使用 ts-jest 实现相同的目标,我尝试在 jest.config.js 文件中使用以下 moduleNameMapper:
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/src/'],
modulePathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/build/'],
testMatch: ['**/*.spec.ts'],
moduleNameMapper: {
"^\@(.*)": "<rootDir>/src/$1",
}
}
But I get the following error:但我收到以下错误:
FAIL src/lib/error/BaseError.spec.ts
● Test suite failed to run
Configuration error:
Could not locate module @babel/code-frame mapped as:
C:\data\devel\apps\sgte-it\coordinacion\juridicos\wspjn\src\babel/code-frame.
Please check your configuration for these entries:
{
"moduleNameMapper": {
"/^@(.*)/": "C:\data\devel\apps\sgte-it\coordinacion\juridicos\wspjn\src\$1"
},
"resolver": null
}
at createNoMappedModuleFoundError (node_modules/jest-resolve/build/index.js:501:17)
On the other hand, if I map every folder at /src like this:另一方面,如果我像这样映射 /src 中的每个文件夹:
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/src/'],
modulePathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/build/'],
testMatch: ['**/*.spec.ts'],
moduleNameMapper: {
"@db/(.*)": "<rootDir>/src/db/$1",
"@lib/(.*)": "<rootDir>/src/lib/$1",
"@modules/(.*)": "<rootDir>/src/modules/$1",
"@services/(.*)": "<rootDir>/src/services/$1"
}
}
It works ok, but I have to update it with every new root folder I add.它工作正常,但我必须用我添加的每个新根文件夹更新它。
Is there any standard, recommended way to achieve such a thing?有没有标准的、推荐的方法来实现这样的事情? I want to have a special character (@ in this case) pointing to my /src/ folder.
我希望有一个特殊字符(在本例中为 @)指向我的 /src/ 文件夹。
Also tried replacing '@' with '$' but had other errors too...还尝试用 '$' 替换 '@' 但也有其他错误......
I made it work with the following config.我使用以下配置使其工作。
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1",
"^@components/(.*)$": "<rootDir>/src/components/$1",
},
This article was helpful.这篇文章很有帮助。
In my case I wanted map @
to ./src
and @components
to ./src/components
在我的情况下,我想将
@
映射到./src
和@components
到./src/components
I could solve it, but not with the @
sign, I had to use $
, more over, I had to escape it and enclose it in square brackets, like this:我可以解决它,但不能使用
@
符号,我必须使用$
,而且,我必须将它转义并将其括在方括号中,如下所示:
tsconfig.json: tsconfig.json:
"paths": { /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
"$project/*": ["./../*"],
"$*": ["./*"],
},
jest.config.js: jest.config.js:
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/src/'],
modulePathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/build/'],
testMatch: ['**/*.spec.ts'],
moduleNameMapper: {
"^[\$]project/(.*)": "<rootDir>/$1",
"^[\$](.*)": "<rootDir>/src/$1",
}
}
I hope it might be useful for other people, configuring all these little details is a real pain...我希望它对其他人有用,配置所有这些小细节是一个真正的痛苦......
Damn, I almost had it all working (ttsc and ts-jest works fine), but vscode wont recognize imports mapped with '$', work ok with '@' though...该死的,我几乎可以正常工作(ttsc 和 ts-jest 工作正常),但是 vscode 无法识别用 '$' 映射的导入,尽管使用 '@' 可以正常工作......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.