Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

307
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda