Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

305
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda