简体   繁体   English

将 react-test-renderer 与 react-testing-library 一起使用有什么意义吗?

[英]Is there any point for using react-test-renderer with react-testing-library?

I can't understand why some tutorials use react-test-renderer with react-testing-library ,我不明白为什么有些教程使用react-test-rendererreact-testing-library

As i understand, react-test-renderer is a library that can create a pure object from a react component and convert it to json snapshot!据我了解, react-test-renderer是一个可以从 React 组件创建纯 object 并将其转换为 json 快照的库!

import TestRenderer from 'react-test-renderer';

function Link(props) {
  return <a href={props.page}>{props.children}</a>;
}

const testRenderer = TestRenderer.create(
  <Link page="https://www.facebook.com/">Facebook</Link>
);

expect(testRenderer).toMatchSnapshot();

Now, i can do the same with Testing library :现在,我可以对Testing library做同样的事情:

import { render } from '@testing-library/react;

test('create link snapshot', () => {
    const {container} = 
       render(<Linkpage="https://www.facebook.com/">Facebook</Link>);
    expect(container.firstChild).toMatchSnapshot();
})

I really can't understand why i need to use react-test-renderer along with testing-library , what can react-test-renderer do testing-library can't?我真的不明白为什么我需要将react-test-renderertesting-library一起使用, react-test-renderer可以做什么testing-library不能?

Renderer渲染器

react-test-renderer is the most basic utility library that is shipped with React. react-test-renderer是 React 附带的最基本的实用程序库。 From React documentation:来自 React 文档:

This package provides a React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM or a native mobile environment.这个 package 提供了一个 React 渲染器,可用于将 React 组件渲染为纯 JavaScript 对象,而不依赖于 DOM 或本机移动环境。


@testing-library/react @测试库/反应

React Testing Library is part of the testing-library family, is built on top of react-test-renderer and is made to be a rather lightweight solution. React Testing Library是 testing-library 家族的一部分,建立在react-test-renderer之上,是一个相当轻量级的解决方案。

It provides utilities to test React and follow the idea of avoiding implementations details tests .它提供实用程序来测试 React 并遵循避免实现细节测试的想法。


Enzyme酵素

The Enzyme is much more complex and heavy.要复杂得多,也重得多。 It gives you many more functionalities but not only for the best: it becomes too easy to do implementation details testing.它为您提供了更多功能,但不仅仅是最好的:进行实施细节测试变得太容易了。

*Note that: Testing implementation details is fundamentally bad *Note that: Enzyme is dead. *请注意:测试实施细节从根本上来说是糟糕的*请注意:酶已死。 (There will be no React 18 support). (不会有 React 18 支持)。Read more Here在这里阅读更多


Read More:阅读更多:

You can visit the link below to read more about these tools and their pros and cons.您可以访问下面的链接,详细了解这些工具及其优缺点。 you can also see a complete comparison and experience after using them over years.您还可以在使用多年后看到完整的比较和体验。

https://morintd.medium.com/react-testing-understand-and-chose-the-right-tools-858236d3c4e1 https://morintd.medium.com/react-testing-understand-and-chose-the-right-tools-858236d3c4e1

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM