Reactjs Jest浅层组件测试错误
我想浅渲染一个组件,并检查它是否崩溃。文档告诉我浅层渲染不会深入到子组件中 我有以下代码:Reactjs Jest浅层组件测试错误,reactjs,testing,jestjs,enzyme,Reactjs,Testing,Jestjs,Enzyme,我想浅渲染一个组件,并检查它是否崩溃。文档告诉我浅层渲染不会深入到子组件中 我有以下代码: it("Renders the Compare landing page without crashing", () => { shallow(<Compare />); }); 测试抛出以下错误: 测试不应该忽略,我在这里遗漏了什么?我的解决方案可能不是最好的,但我就是这么做的: 我为匹配创建了一个模拟函数: import {match} from 'react-router'
it("Renders the Compare landing page without crashing", () => {
shallow(<Compare />);
});
测试抛出以下错误:
测试不应该忽略
,我在这里遗漏了什么?我的解决方案可能不是最好的,但我就是这么做的:
我为匹配创建了一个模拟函数:
import {match} from 'react-router';
export function mockedMatch<P = void>(params?: P): match<P> {
return {
params,
isExact: true,
path: 'lorem/ipsum',
url: 'dummyUrl',
};
}
从'react router'导入{match};
导出函数mockedMatch(参数?:P):匹配
{
返回{
params,
是的,
路径:“lorem/ipsum”,
url:'dummyUrl',
};
}
然后在测试时将此模拟传递给组件:
shallow(<Compare match={mockedMatch({gamerId: 'test-id'})} />);
shall();
我的解决方案是使用TypeScript,但您可以删除这些类型,并且它也适用于您的情况。不,测试不会忽略
。您需要将一个值传递给匹配
道具。
shallow(<Compare match={mockedMatch({gamerId: 'test-id'})} />);