简体   繁体   English

react-test-renderer 的 create() 与 @testing-library/react 的 render()

[英]react-test-renderer's create() vs. @testing-library/react's render()

I'm new to React and confused about all the testing libraries.我是 React 新手,对所有测试库感到困惑。 I got my test code to work but it seems redundant to have to call create() from react-test-renderer in order to use its toMatchSnapshot() and have to call render() from @testing-library/react in order to use its assertions such as getByLabelText() .我让我的测试代码可以工作,但是必须从 react-test-renderer 调用create()才能使用它的toMatchSnapshot()并且必须从 @testing-library/react 调用render()才能使用,这似乎是多余的它的断言,例如getByLabelText()

import {render} from '@testing-library/react';
import {act, create} from 'react-test-renderer';

it('renders a login screen', () => {
    let mockInitialState: AppState = {
        auth: initialAuthState
    };

    let component = <Root initialState={mockInitialState}/>;

    let tree = null;
    act(() => {
        tree = create(component);
    });
    expect(tree).toMatchSnapshot();

    const {getByLabelText, getByText} = render(component);
    expect(getByLabelText(/Email Address.*/));
    expect(getByLabelText(/Password*/));
    expect(getByText('Sign in'));
});

As a newbie, it's hard for me to understand the difference between all these React libraries.作为一个新手,我很难理解所有这些 React 库之间的区别。 But I'm thinking there must be a simpler way.但我认为必须有一个更简单的方法。

How can I simplify my test code so I only have to call one thing that renders the component so that I can do snapshot testing and more specific assertions?我怎样才能简化我的测试代码,所以我只需要调用一个渲染组件的东西,以便我可以进行快照测试和更具体的断言?

I got the answer from Ziad Saab at Codementor.io:我从 Codementor.io 的 Ziad Saab 那里得到了答案:

  • create() allows you test against the virtual DOM (ie the "React DOM") create()允许您针对虚拟 DOM(即“React DOM”)进行测试

  • render() comes from react testing library and renders your tree but also allows you to have all the get*() assertions. render()来自反应测试库并呈现您的树,但也允许您拥有所有 get*() 断言。 It allows you to test against the DOM.它允许您针对 DOM 进行测试。

Here's how the code can be simplified:下面是如何简化代码:

it('renders a login screen', () => {
    let mockInitialState: AppState = {
        auth: initialAuthState
    };

    const {container, getByLabelText, getByText} = render(<Root initialState={mockInitialState}/>);
    expect(container.firstChild).toMatchSnapshot();
    expect(getByLabelText(/Email Address.*/));
    expect(getByLabelText(/Password*/));
    expect(getByText('Sign in'));
});

Ziad let me know that there was no reason to have act() , it was something to work around a bug in create() . Ziad 让我知道没有理由拥有act() ,它可以解决create()中的错误。 Now that the code doesn't used create() there is no need for act() .既然代码没有使用create() ,就不需要act()

As a result, my snapshot now contains class instead of className because class is what's in the actual HTML DOM whereas className is its equivalent in React's "Virtual DOM".结果,我的快照现在包含class而不是className因为class是实际 HTML DOM 中的内容,而className是 React 的“虚拟 DOM”中的等价物。

(Before) Snapshot with create() based on React's Virtual DOM: (之前)基于 React 的 Virtual DOM 的create() ) 快照:

className="MuiBox-root MuiBox-root-256"

(After) Snapshot with render() based on HTML DOM: (之后)基于 HTML DOM 的render() ) 快照:

class="MuiBox-root MuiBox-root-256"

If you're using Create React App then I'd stick with react-testing-library since it comes with it.如果您使用的是 Create React App,那么我会坚持使用 react-testing-library,因为它带有它。

Instead of container, you can also use asFragment for snapshot testing.除了容器之外,您还可以使用asFragment进行快照测试。

 const {container} = render(<Root initialState={mockInitialState}/>);
 expect(asFragment).toMatchSnapshot();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用@testing-library/react 和 react-test-renderer 测试由 Redux 状态控制的输入值? - how to test input value controlled by Redux state using @testing-library/react and react-test-renderer? 使用react-test-renderer进行快照测试 - snapshot testing with react-test-renderer 使用 react-test-renderer 测试异步 componentDidMount() - Testing async componentDidMount() with react-test-renderer 使用 React-Test-Renderer 测试嵌套的 React 组件 - Testing of Nested React components using React-Test-Renderer 将 react-test-renderer 与 react-testing-library 一起使用有什么意义吗? - Is there any point for using react-test-renderer with react-testing-library? 无法使用 Jest 和 Testing-Library 测试 React 组件渲染,因为 Jest 没有可用的文档 - Trouble testing React component render with Jest and Testing-Library because there's no Document available to Jest 使用@testing-library/react 对 react-router 的链接进行最简单的测试 - Simplest test for react-router's Link with @testing-library/react 测试 Formik 使用 react-test-renderer 验证 function - Testing Formik validate function with react-test-renderer 为什么此测试使用Jest和react-test-renderer引发错误,因为它试图解析.graphql文件? - Why is this test using Jest & react-test-renderer throwing error because it's trying to parse a .graphql file? 使用 testing-library/react-native 接收号码的测试道具 - test prop that's receiving number with testing-library/react-native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM