【发布时间】:2020-06-02 21:09:12
【问题描述】:
所以我有一个使用的反应组件:
const MyPage = DynamicImport({
id: 'MyPage',
loader: () => import('./pages/MyPage/MyPageWrapper'),
});
开玩笑地抱怨说我的测试没有涵盖加载程序,第 22 行:
然后在同一个文件中使用它:
const tabs = useMemo(
() => [
{
label: 'myPage',
url: 'myRoute',
component: MyPage,
},
...
最后
{tabs.map(({ url, component }) => {
return <Route path={url} component={component} key={url} />;
})}
我正在学习如何编写单元测试,但我不知道如何在屏幕截图上用红线覆盖这一部分,显然我一直在谷歌搜索并寻找答案,但我找不到任何有用的东西。
这是我的测试文件:
import React from 'react';
import { MyComponent } from '../MyComponent';
import { makeSetupComponent } from '../../../../test_utils';
const MyPage = () => ({
id: '',
loader: jest.fn(),
});
jest.mock('../pages/MyPage/MyPageWrapper', () => {
return {
MyPageWrapper: jest.fn(),
};
});
const defaultProps = {
...
};
const setup = makeSetupComponent({
props: defaultProps,
shallow: true,
component: MyComponent,
});
describe('MyComponent component', () => {
test('Renders correctly', () => {
const { component } = setup();
expect(component.exists()).toBe(true);
});
test('render MyPage', () => {
const { component } = setup({ shallow: false });
console.log('###DEBUG', component.debug());
console.log('###MyPage', MyPage);
const { loader } = MyPage();
loader();
expect(MyPage).toBeInstanceOf(Function);
expect(loader).toHaveBeenCalled();
});
});
任何帮助/建议/链接/课程-完全覆盖-这将不胜感激
【问题讨论】:
-
测试是什么?给minimal reproducible example。
-
@jonrsharpe 我刚刚编辑了添加我的测试文件的帖子,这是我的尝试,但当然,这段代码没有覆盖你在我的截图上看到的红线,这就是我的目标.
-
不清楚你为什么会有其他期望;看起来一切都被嘲笑了,没有什么可以测试了。
-
@jonrsharpe 抱歉,说清楚我想增加覆盖率,如果你查看我帖子中的屏幕截图,它会说第 22 行没有被覆盖,我希望覆盖它,所以我是询问是否有人知道如何实现它
-
是的,这很清楚。您是否尝试过测试实际的 MyPage 而不是您在测试中构建的假的?
标签: javascript reactjs unit-testing jestjs