用于 react 测试的 enzyme 、ReactTestUtils和react-testing-library之间有什么区别?
ReactTestUtils 文档说:
ReactTestUtils makes it easy to test React components in the testing framework of your choice.
enzyme 文档只是说:
Enzyme is a JavaScript Testing utility for React that makes it easier to assert, manipulate, and traverse your React Components' output.
React-testing-library 文档:
The react-testing-library is a very light-weight solution for testing React components. It provides light utility functions on top of react-dom.
为什么实际上每种解决方案都更容易,而另一种解决方案却无法实现什么?
最佳答案
ReactTestUtils 为您提供了测试 React 组件的最低限度。我还没有看到它被用于大型应用程序。
Enzyme 和 React-testing-library 都是很好的库,可以为您提供测试应用程序所需的所有工具。但他们有两种不同的理念。
Enzyme 允许您访问组件的内部运作。您可以读取和设置状态,并且可以模拟子级以使测试运行得更快。
另一方面,react-testing-library 不允许您访问实现细节。它呈现组件并提供与它们交互的实用方法。我们的想法是,您应该以与用户相同的方式与应用程序进行通信。因此,您无需设置组件的状态,而是重现用户为达到该状态而执行的操作。
根据我的经验, enzyme 更容易掌握,但从长远来看,它更难维护。 React-testing-library 会迫使您编写平均来说稍微复杂一点的测试,但它会让您对代码更有信心。
关于reactjs - enzyme 、ReactTestUtils 和 React-testing-library 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54152562/