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?