[英]module alias cannot resolve for jest (CRA and customize-cra)
I create a react app with CRA and add react-app-rewired
and TypeScript
I map some modules in tsconfig.json
我用CRA创建了一个react应用,并添加了
react-app-rewired
和TypeScript
我在tsconfig.json
映射了一些模块
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@constants/*": ["constants/*"],
"@components/*": ["components/*"],
"@grid/*": ["components/Grid/*"],
"@grid-share/*": ["components/Grid/Share/*"],
"@utils/*": ["util/*"],
"@services/*": ["Services/*"]
}
},
"extends": "../tsconfig.json"
}
and also define them as alias modules in config-overrides.js
并在
config-overrides.js
中将它们定义为别名模块
const path = require('path');
const { override, addBabelPlugins } = require('customize-cra');
module.exports = override(
...addBabelPlugins(
[
'module-resolver',
{
root: ["./src"],
alias: {
"@constants": "./src/constants",
"@components": "./src/components",
"@grid": "./src/components/grid",
"@utils": "./src/util",
"@services": "./src/Services",
"@grid-share": "./src/components/Grid/Share"
}
}
],
),
);
everything is okey for yarn start
but for yarn test
, it cannot resolve the defined modules. 一切对于
yarn start
都是好的,但是对于yarn test
,它无法解析已定义的模块。 what should I do for resolving the alias module for jest
? 解决
jest
的别名模块该怎么办?
Okey, I found the answer. Okey,我找到了答案。 we need to define same module alias in package.json
我们需要在package.json中定义相同的模块别名
"jest": {
"moduleNameMapper": {
"@constants/(.*)": "<rootDir>/src/constants/$1",
"@utils/(.*)": "<rootDir>/src/util/$1",
"@grid-share/(.*)": "<rootDir>/src/components/Grid/Share/$1",
"@grid/(.*)": "<rootDir>/src/components/Grid/$1"
}
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.