question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

document.querySelectorAll returns stale results

See original GitHub issue

Basic info:

  • Node.js version: v8.11.4
  • jsdom version: v13.2.0 (bug is also present in v11.12.0)

Minimal reproduction case

const jsdom = require('jsdom');
const {JSDOM} = jsdom;

function append(html) {
  document.body.innerHTML = html;
}

function clean() {
  document.body.innerHTML = '';
}

const dom = new JSDOM(`<!DOCTYPE html>`);
const document = dom.window.document;

const fixtures = {
  a: '<a id="link-a" class="test">a link</a>',
  b: '<a id="link-b" class="test"><img src="foo.jpg" /></a>'
};

append(fixtures.a);
console.log(document.body.innerHTML);
// should log "link-a"
document.querySelectorAll('a.test').forEach(node => console.log(node.id));
clean();

append(fixtures.b);
console.log(document.body.innerHTML);
// should log "link-b"
document.querySelectorAll('a.test').forEach(node => console.log(node.id));
clean();

append(fixtures.a);
console.log(document.body.innerHTML);
// should log "link-a", but logs "link-b"
document.querySelectorAll('a.test').forEach(node => console.log(node.id));
clean();

How does similar code behave in browsers?

https://jsbin.com/fobuxoceva/edit?js,console

Pretty much what the title says. document.querySelectorAll returns Nodes which have been removed already. I suspect this is an issue of memoization of queries: If the last querySelectorAll('a.test') is changed to querySelectorAll('.test') (parameters are different from previous calls), it works as expected. AFAIK memoization for querySelectorAll has been removed here: https://github.com/jsdom/jsdom/commit/bfb08434f540491591aace17b302816f146dd4ef

Context

We discovered this while move old jasmine tests over to jest which uses jsdom. I guess adding and removing DOM nodes between unit tests isn’t anything exotic.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
domeniccommented, Feb 23, 2019

Some discussion going on in https://github.com/dperini/nwsapi/pull/23; hopefully that gets fixed in nwsapi as this kind of breaking change in a minor version is not really OK 😦.

0reactions
ghostcommented, Mar 1, 2019

@sscaetite This looks correct and unrelated to this issue. You shouldn’t trust console.log to display the contents of your divs. Try to log the actual HTML using node.outerHTML.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Document.querySelectorAll() - Web APIs | MDN
The Document method querySelectorAll() returns a static (not live) NodeList representing a list of the document's elements that match the ...
Read more >
javascript - QuerySelectorAll returns more than expected
You can use this selector: document.querySelectorAll(".main [class='sub']"). You can iterate over result items as follows:.
Read more >
HTML DOM Document querySelectorAll() Method
The querySelectorAll() method returns all elements that matches a CSS selector(s). The querySelectorAll() method returns a NodeList.
Read more >
Javascript: Manipulating DOM (HTML & CSS) - Mr B
Returns a variable containing a reference to the first matching location within your HTML document as determined by your selector_string .
Read more >
HTML | DOM querySelectorAll() Method
The querySelectorAll() method in HTML is used to return a collection of an ... < body style = "text-align: center;" > ... var...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found