【发布时间】:2018-07-10 07:28:08
【问题描述】:
我使用create-react-app 和react-scripts-ts 生成了一个react 应用程序,并弹出以配置webpack。我正在使用react-async-component 异步渲染页面。
import { asyncComponent } from 'react-async-component';
const AsyncNotFoundPage = asyncComponent({
resolve: () => import('./NotFoundPage').then(x => x.default)
});
export { AsyncNotFoundPage };
当我启动应用程序时,它会按预期工作。它拆分未找到页面的块。但是,以下开玩笑的测试用例给出了错误;
describe('AsyncNotFoundPage Component', () => {
it('should render async', () => {
const wrapper = shallow(<AsyncNotFoundPage />);
expect(wrapper.find(NotFoundPage)).toBeTruthy();
});
});
它给出了以下错误;
Unexpected token import
我尝试将所有动态导入插件添加到 package.json 中的 babel 配置中。它不起作用。
有什么想法吗?
【问题讨论】:
-
你尝试过 require('moduleName').我遇到了同样的错误,它通过使用 require('') 解决了
-
你能分享 babel 配置吗.. 我正在添加一个通用解决方案。希望它有效
-
@hannadrehman babel config 默认只有
react-app预设 -
你可以试试下面的答案。我确定是因为测试环境没有动态导入插件
标签: reactjs typescript babeljs jestjs create-react-app