为什么React需要jsdom进行测试? [英] Why does React require jsdom for testing?

查看:170
本文介绍了为什么React需要jsdom进行测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为React组件编写测试时,必须将它们渲染到DOM中,以便对其正确性做出断言。例如,如果要测试某个类被添加到给定某个状态的节点,则必须将其渲染到DOM节点中,然后通过常规DOM API检查该DOM节点。

When writing tests for React components, you have to render them into the DOM in order to make assertions about their correctness. For example, if you want to test that a certain class is added to a node given a certain state, you have to render into a DOM node, then inspect that DOM node via the normal DOM API.

事情是,考虑到React维护它呈现的虚拟DOM,为什么一旦渲染组件,我们不能在虚拟DOM上断言?这似乎是一个非常好的理由,有一些像虚拟DOM。

The thing is, considering React maintains a virtual DOM into which it renders, why can't we just assert on the virtual DOM once the component is rendered? That seems to me like a very good reason to have something like the virtual DOM.

我错过了什么?

推荐答案

你没有真的错过任何东西。我们正在努力使这个更好。虚拟部分一直是React的实现细节,不以任何有用或可靠的方式进行测试。我们在测试帮助中有一些方法,其中包含内部查找,有时避免查看实际的DOM,但是我们需要更多。

You haven't really missed anything. We're working on making this better. The virtual parts have always been very much an implementation detail of React, not exposed in any useful or reliable way for testing. We have some methods in our test helpers which wrap up the internal lookups which sometimes avoids looking at the actual DOM but we need more.

这篇关于为什么React需要jsdom进行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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