首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用withRouter时,代码覆盖率返回0%

使用withRouter时,代码覆盖率返回0%
EN

Stack Overflow用户
提问于 2019-06-10 21:10:39
回答 2查看 81关注 0票数 0

快速总结-我有一个React应用程序。单元测试库是react测试库。该组件被封装在来自withRouter -router-dom的react中

问题-代码覆盖率显示为0%,即使它显示通过了8个测试和一些跳过的信息。如果我从withRouter中删除组件,代码覆盖率将显示正确的覆盖率结果。

请检查下面的代码,我正在尝试匹配快照。

代码语言:javascript
复制
// Profile.test.js
import React from "react";
import Profile from "./Profile";
import { withRouter } from "react-router-dom";
import { render } from "react-testing-library";

it("renders the component", async () => {
  const container = withRouter(<Profile />);
  expect(container).toMatchSnapshot();
});

// Profile.js
import react from 'react';
import { withRouter } from "react-router-dom";

const Profile = () => {
    return (
        <div>The profile component</div>
    )
}

export default withRouter(Profile);

我应该能够对组件使用withRouter并查看覆盖率。

EN

回答 2

Stack Overflow用户

发布于 2019-06-10 21:31:01

我认为你的测试应该是这样的:

代码语言:javascript
复制
it('renders the component', () => {
  const component = mount(<BrowserRouter><Profile /></BrowserRouter>);
  expect(component).toMatchSnapshot();
});
票数 0
EN

Stack Overflow用户

发布于 2019-06-10 23:40:59

withRouter旨在在您的应用程序中用于注入路由器数据。

在测试中,您必须将路由器呈现为组件树的一部分:

代码语言:javascript
复制
import { MemoryRouter } from 'react-router-dom';

it('renders the component', () => {
  const { container } = render(<MemoryRouter><Profile /></MemoryRouter>);
  expect(container).toMatchSnapshot();
});

如果您想确保您的应用程序正常工作,我建议您不要只使用依赖于快照测试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56527094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档