Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

310
Views
How to initalize the DOM using jsdom and Jest?

I am currently trying to test my nodejs app with jest. I have some elements in the HTML for a specific page that the file that I want to test references. The problem is that when I import the file with the functions I want to test, it's run through once. In this run-through, there is no DOM yet, so the querySelected elements are null. Is there any way to initialize the the document.body before the import statements read through the file that the functions are coming from?

Currently, the top of the test file looks something like this:

/**
 * @jest-environment jsdom
 */

 const fs = require('fs');
 document.body.innerHTML = fs.readFileSync("./public/Views/index.html");

const {model} = require('./model.js')
import { something, Item } from './model.js'
import { addThis, notifications, navigateToPage } from './indexScript.js'

When the test is run, the files which are being imported use a querySelector(), but they are never able to select anything because the jsdom is just intialized to:

'<!DOCTYPE html>'

Is there a way to initialize the document body so that there is something there before the import statements happen?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!