我正在构建由创建-反应-应用程序提供的react应用程序。
我使用craco来简化配置,并为TypeScript和Webpack设置了alise。
但是,当我运行test命令时,会显示以下错误。
spec/showMessage.test.tsx
● Test suite failed to run
Cannot find module '@/components/atom/TextButton' from 'src/pages/index.tsx'
Require stack:
src/pages/index.tsx
spec/showMessage.test.tsx
3 | import styled from 'styled-components';
4 |
> 5 | import { TextButton } from '@/components/atom/TextButton';
| ^这是我的craco.config.js
const path = require("path");
module.exports = {
jest: {
configure: {
roots: ['<rootDir>/src', '<rootDir>/spec'],
testMatch: ['<rootDir>/spec/**/*.{spec,test}.{js,jsx,ts,tsx}'],
},
},
webpack: {
alias: {
"@": path.resolve(__dirname, "./src/"),
"@@": path.resolve(__dirname, "./")
},
extensions: ['.ts', '.tsx'],
},
};发布于 2021-01-18 05:49:45
我找到了解决方案,我像下面这样更新了package.json,它解决了这个问题。
{
"name": "xxxxx",
:
},
"jest": {
"moduleNameMapper": {
"^@/(.+)": "<rootDir>/src/$1"
}
}
}https://stackoverflow.com/questions/65767551
复制相似问题