Your unit tests are paying an unnecessary DOM tax

InfoWorld ·

Your unit tests are paying an unnecessary DOM tax

Open the test configuration of almost any front-end project and you’ll find one line that decides more about your CI bill than most of the code around it: environment: 'jsdom' . Unit and component tests for React, Vue, Angular, Svelte, and Solid apps rarely run in a real browser. They run in Node.js, on top of a JavaScript implementation of the browser’s Document Object Model. Your test renders a button, clicks it, and asserts on the text that appears. The document, the click() , and the text all come from a library that pretends to be a browser. For most teams that library is jsdom , the long-standing, standards-minded default. A growing number use happy-dom , which is faster but less faithful. Few teams think about the choice again after the first npm install. They should, because the test DOM is often where a component suite spends a large share of its time, and because the two popular options force a trade that most teams don’t know they’re making. Over the past months I measured this on real projects. Here is what I found, and what I built as a result. Where the time goes A component test does very little work of its own. It creates elements, sets attributes, dispatches events, and reads back state. Every one of those steps is a call into the DOM implementation, and DOM APIs are chatty: a single high-level call can fan out into hundreds of internal ones. To see how chatty, I built an open-source tracer, dom-api-tracer , that records which DOM APIs a test suite calls and how often. One example from those traces: a single getComputedStyle() call in jsdom triggered about 1,000 reads of Node and Element properties inside the library, because resolving styles means matching selectors against the tree. Testing libraries call getComputedStyle() constantly, for instance to decide whether an element is visible. Multiply that by tens of thousands of tests and the DOM is no longer a detail. On Ant Design’s suite of React components, swapping the DOM implementation alone took a CI run from about 30 minutes to 6.7 minutes. Nothing else changed: same tests, same runner, same machine. There’s a second cost that has nothing to do with DOM calls. Test runners such as Vitest isolate every test file by default, which means the framework, the component library, and the DOM environment are set up again for each file. In a large component suite, that reloading can take more time than the tests themselves. The trade nobody chose If jsdom is slow, why not switch to happy-dom? Many projects have, and on speed alone it delivers. In my measurements happy-dom ran a median of about 1.25x to 1.28x faster than jsdom across 13 open-source suites. But it changed test results in 12 of those 13 suites. Tests that passed on jsdom failed on happy-dom, and in some cases tests that failed on jsdom passed on happy-dom. One suite had 4,681 tests break, which is also why it “ran faster.” None of this is a criticism of happy-dom’s engineers. Implementing the DOM is a huge job, and every implementation makes choices about which corners of the specification to cover and how closely. The point is that a test suite encodes the behavior of the DOM it was written against. Change the DOM, and you change what your tests mean. A test that flips from red to green after an environment switch is no better than one that flips from green to red. In both cases you no longer know what your test suite is telling you. It goes further than jsdom versus happy-dom. Upgrading jsdom alone changed test results in 13 of the 23 projects I measured. For example, before jsdom v29, getComputedStyle() returned empty strings for most CSS properties; since then it returns initial values. Tests that compared against empty strings, sometimes without their authors knowing it, broke on the upgrade. One project had 258 tests change status between jsdom versions. So teams face three uncomfortable options: stay on jsdom and pay for it in CI minutes, move to happy-dom and audit every changed test, or stay pinned to an old jsdom and hope. A different approach: same results, faster I wanted to know whether that trade was fundamental, or just a consequence of how existing implementations were built. So I wrote warpdom , an open-source DOM for unit and component tests, written from the WHATWG specifications , that ships as a drop-in environment for Vitest and Jest. Its goal is narrow on purpose: your tests get the same results as on jsdom, only faster . Three design decisions follow from it. warpdom follows your jsdom version. Because test suites encode the behavior of the jsdom version they were written against, warpdom detects the jsdom version a project has installed and reproduces that version’s observable behavior, from jsdom v20 to the current release. If your project used jsdom v26, getComputedStyle() keeps behaving the way it did in jsdom v26. Each of those version boundaries was measured black-box against jsdom’s releases. warpdom is built for test workloads. Test suites build and tear down thousands of small trees. warpdom keeps its tree in a compact internal form, creates JavaScript node objects only when test code actually touches them, and keeps environment startup close to free. Which code paths deserved optimization came from the tracer data, not from guesses. warpdom is a single line to switch, and a single command to check. Before changing anything, npx warpdom compare runs your suite on jsdom and on warpdom side by side and reports every test whose result differs, plus the speedup. It doesn’t modify your project. If you like what you see, npx warpdom migrate makes the configuration change. What the measurements show All of the following numbers come from GitHub Actions runs on hosted Linux runners, using each project’s own test runner, configuration, and jsdom version, at pinned commits. The DOMs were alternated on the same machine over several rounds, and the raw data and scripts are public. Same results. Across 23 open-source suites, about 57,000 tests including MUI, React Spectrum, Radix, Vuetify, Handsontable, and Svelte, every test had the same status as on jsdom, apart from flaky tests and a short, documented list of differences. In 17 of 25 suites (including two applications), not a single test differed in any round. Faster on every suite. As a pure drop-in replacement, warpdom was faster than jsdom on all 13 suites I tuned against, in each of three independent runs. The median speedup was about 1.27x, ranging from 1.03x (Solid) to 1.89x (MUI). On eight held-out suites I didn’t tune against, the median was 1.36x. Much faster on older jsdom setups. Handsontable’s core, which runs jsdom 16.7, went from 593 seconds to 69 seconds, roughly 8x to 9x. More standards coverage, not less. On 17 areas of the Web Platform Tests that a test DOM is expected to cover, warpdom passes 75.4% of subtests, against 72.2% for the current jsdom. It’s still behind jsdom in one area, HTML forms, and that work is under way. A drop-in speedup of 1.3x may not sound dramatic. It’s the floor, though, not the ceiling, and it comes without touching a single test. Removing the reload tax The larger gains come from the second cost: reloading modules for every test file. Turning off isolation in Vitest is a known trick, but it’s risky, because tests start sharing state and order-dependent failures appear. warpdom has an optional fast mode for Vitest that runs files without isolation and resets the DOM window between them. Files that still leak module state are detected and run isolated. On 16 open-source Vitest suites, fast mode was 9.6x faster than jsdom’s default on VueUse, 6.7x on MUI, 5.6x on Vuetify, and 4.7x on Radix. Those gains come with a caveat. How much fast mode helps depends entirely on how much of a suite’s time goes into reloading modules. The median across all 16 suites was 1.54x over jsdom’s default, and essentially no gain over warpdom’s own drop-in mode on the suites where sharing doesn’t pay. Against the fastest jsdom configuration that still kept every result correct, fast mode was slower on two suites. Measure it on your own suite before you rely on it. What to check in your own project Whether or not you try warpdom, four questions are worth asking about your test DOM: How much of your CI time is DOM time? Profile one test run. If your suite is component-heavy, the answer is probably more than you’d expect. Are you pinned to an old jsdom because upgrading broke tests? That’s a sign your tests encode version-specific behavior. Know which tests those are before your next upgrade. If you moved to happy-dom, did you check what changed? Run the suite on both jsdom and happy-dom and compare test statuses, not just pass counts. A suite can keep the same number of passing tests while different tests pass. Does your environment support the APIs your code uses? Test DOMs vary widely in which web platform APIs they implement. To make that visible, I also started DOMParity , a neutral reference site that shows API by API how jsdom, happy-dom, warpdom, and linkedom support the web platform, using the same black-box checks for every engine, including where warpdom falls short. Where this is going warpdom is young. It’s pre-1.0 and has far less mileage than jsdom, which has served the JavaScript ecosystem for well over a decade and on whose observable behavior warpdom deliberately builds. Like jsdom, warpdom has no layout engine, and some APIs are still missing. Each gap is tracked as a public issue. APIs that jsdom doesn’t have will come as opt-in features, so that default results stay identical to jsdom’s. The broader lesson goes beyond any one library. The test DOM is infrastructure, and we’ve been treating it as a detail. Choosing one shouldn’t mean choosing between fast tests and tests you can trust. With measurement instead of guesswork, it doesn’t have to.

Источник: InfoWorld