酶和反应测试库之间的差异
酶和React测试库之间有什么区别,我应该在React项目中使用哪一个?
酶: https://www.npmjs.coms.com/package/package/enzyme/enzyme
反应测试库:<<< a href =“ https://www.npmjs.com/package/@testing-library/react” a>
What is the difference between Enzyme and React Testing Library and which one should I use for testing in React project?
Enzyme: https://www.npmjs.com/package/enzyme
React Testing Library: https://www.npmjs.com/package/@testing-library/react
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在酶中,我们使用组件的状态和道具测试组件。这通常意味着测试是脆弱的。假设我们已经为组件写了测试,并且运行良好。但是,如果某人更改组件中状态或道具的变量名称,那么即使组件的功能没有更改,我们的测试也会失败。这种行为证明了测试的脆弱性。
而在React测试库中,我们从用户的角度测试组件。假设我们要测试下拉组件,我们不会根据其状态和道具测试该组件。相反,我们使用DOM元素对其进行测试,就像用户如何与之互动。
React测试库的主要目的是通过用用户使用的方式来测试组件来提高对测试的信心。用户不在乎幕后会发生什么,他们只是看到并与输出互动。与其访问组件的内部API或评估其状态,不如通过根据组件输出编写测试来获得更大的信心。
在某个时候,反应测试文库比酶更广泛使用。
In Enzyme, we test the component using the state and props of the component. This generally means that the tests are brittle. Let's say we have already written tests for a component and it is running fine. But what if someone changes the variable name of a state or props in the component, then even if the functionality of the component hasn't changed, our test fails. This kind of behavior demonstrates the brittleness of the test.
Whereas in React Testing Library, we test the component from the user's perspective. Let's say we want to test a drop-down component, we won't test the component according to its state and props. Instead, we use the DOM elements to test it, just how the user interacts with it.
The primary purpose of React Testing Library is to increase confidence in your tests by testing your components in the way a user would use them. Users don't care what happens behind the scenes, they just see and interact with the output. Instead of accessing the components' internal APIs or evaluating their state, you'll get more confidence by writing your tests based on the component output.
At some point, React Testing Library became more widely used than Enzyme.