【发布时间】:2019-07-07 18:43:02
【问题描述】:
我在 Jest 测试 React 惰性组件时遇到问题。惰性组件不解析为 React 组件,而是惰性对象,因此它会引发 Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. 错误。我如何让它们解决,以便它们可以成为要测试的元素?我的设置如下。
我正在使用 React-Router 和 Redux。尝试测试某些组件是否出现在每条路线中。
测试包装函数设置如下:
const mountWithPath = async (path, newProps = {}) => {
const wrapper = mount(
<MemoryRouter initialEntries={[path]}>
<Provider store={store}>
<Suspense fallback={<div />}>
<CompAppNoRouter {...modProps} />
</Suspense>
</Provider>
</MemoryRouter>
);
await People;
await DashboardPage;
await ActivityPage;
await Analysis;
await Upload;
return wrapper;
将延迟加载的组件导入测试:
import { People, DashboardPage, ActivityPage, Analysis, Upload } from '../app';
来自app.jsx的导出:
export const People = lazy(() => import('./pages/people/people'));
export const DashboardPage = lazy(() => import('./pages/dashboard/dashboard'));
export const ActivityPage = lazy(() => import('./pages/activity-report/activity-report'));
export const Analysis = lazy(() => import('./pages/analysis/analysis'));
export const Upload = lazy(() => import('./pages/upload'));
【问题讨论】:
标签: reactjs react-router jestjs lazy-loading