Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does React require jsdom for testing?

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.

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.

Have I missed something?

like image 232
Tom Avatar asked Oct 08 '14 08:10

Tom


1 Answers

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.

like image 146
Paul O'Shannessy Avatar answered Oct 22 '22 06:10

Paul O'Shannessy