如何解决如何使用 craco
我正在构建由 create-react-app 提供的 React 应用。
我使用 craco 来简化配置,并为 TypeScript 和 Webpack 设置了 alise。
但是当我运行测试命令时,显示以下错误。
- 错误信息
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
- 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'],};
解决方法
我找到了解决方案,我更新了 package.json
如下所示,它解决了这个问题。
{
"name": "xxxxx",:
},"jest": {
"moduleNameMapper": {
"^@/(.+)": "<rootDir>/src/$1"
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。