node.jsjsdomnative-methods

jsdom - document.querySelector enabled, but is missing from document


I would like to use JSDom to perform some server-wise DOM manipulation. However, despite explcitly enabling querySelector, it is undefined in the documents created:

var jsdom = require('jsdom');

// Yep, we've got QuerySelector turned on
jsdom.defaultDocumentFeatures = {
  QuerySelector: true
};

var dom = jsdom.defaultLevel;

var document = jsdom.jsdom("<html><body><h1>Hello StackOverflow</h1></body></html>"),
window = document.createWindow();

However:

console.log(document.querySelector)

Returns

undefined

How can I make document.querySelector work properly using jsdom?


Solution

  • Following the JSDOM documentation, here the updated code for version 16.6.0:

    const { JSDOM } = require("jsdom");
    
    const dom = new JSDOM("<html><body><h1>Hello</h1></body></html>");
    const document = dom.window.document;
    
    console.log(document.querySelector);