I have a node project with typescript, and I have the following paths configured in my tsconfig.json file:
(我有一个带有Typescript的节点项目,并且在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:datadevelappssgte-itcoordinacionjuridicoswspjnsrcabel/code-frame.
Please check your configuration for these entries:
{
"moduleNameMapper": {
"/^@(.*)/": "C:datadevelappssgte-itcoordinacionjuridicoswspjnsrc$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...
(还尝试用'$'替换'@',但也有其他错误...)
ask by opensas translate from so