酶,ReactTestUtils和反应测试库之间的区别 [英] Difference between enzyme, ReactTestUtils and react-testing-library

查看:88
本文介绍了酶,ReactTestUtils和反应测试库之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于反应测试的酶,ReactTestUtils和react-testing-library之间有什么区别?

What is the difference between enzyme, ReactTestUtils and react-testing-library for react testing?

ReactTestUtils文档说:

The ReactTestUtils documentation says:


ReactTestUtils使您可以轻松地在您选择的测试
框架中测试React组件。

ReactTestUtils makes it easy to test React components in the testing framework of your choice.

酶文档只是说:


Enzyme是用于React的JavaScript测试实用程序,它使
更容易来声明,操纵和遍历React组件的输出。

Enzyme is a JavaScript Testing utility for React that makes it easier to assert, manipulate, and traverse your React Components' output.

反应测试库文档:


react-testing-library是用于测试
React组件的轻量级解决方案。它在
react-dom之上提供了轻量级的实用程序功能。

The react-testing-library is a very light-weight solution for testing React components. It provides light utility functions on top of react-dom.

为什么每个解决方案实际上都更容易,哪些不能解决?

Why is actually every solution easier and what can't be achieved with the other one?

推荐答案

ReactTestUtils为您提供了测试React组件的最低要求。我还没有看到它被用于大型应用程序。

ReactTestUtils gives you the bare minimum to test a React component. I haven't seen it being used for big applications.

酶和react-testing-library都是很好的库,可以为您提供测试应用程序所需的所有工具。但是,他们有两种不同的哲学。

Enzyme and react-testing-library are both good libraries that give you all the tools you need to test your application. They have two different philosophies though.

酶允许您访问组件的内部工作原理。您可以读取并设置状态,还可以嘲笑孩子以使测试运行更快。

Enzyme allows you to access the internal workings of your components. You can read and set the state, and you can mock children to make tests run faster.

另一方面,react-testing-library不会给您任何帮助访问实施细节。它呈现了组件并提供了与之交互的实用方法。这个想法是,您应该以与用户相同的方式与应用程序通信。因此,与其设置组件的状态,不如重现用户为达到该状态所要做的动作。

On the other hand, react-testing-library doesn't give you any access to the implementation details. It renders the components and provides utility methods to interact with them. The idea is that you should communicate with your application in the same way a user would. So rather than set the state of a component you reproduce the actions a user would do to reach that state.

以我的经验,酶更容易掌握,但从长远来看,很难维护。 react-testing-library迫使您编写平均而言更复杂的测试,但它会给您带来更高的代码信心。

In my experience Enzyme is easier to grasp but in the long run, it's harder to maintain. react-testing-library forces you to write tests that are a bit more complex on average but it rewards you with higher confidence in your code.

这篇关于酶,ReactTestUtils和反应测试库之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆