I have a simple http Express server that serves a couple of routes like home, about, news, etc.
For each of routes, I use res.render('some_ejs_file'), to render a html page with .ejs extension. each page has a .js file script, and I just wonder how can I render the .ejs files to run the proper js file and render the page to test it?
I installed the @testing-library and jest and here is my home.test.js file:
import '@testing-library/jest-dom/extend-expect'
import { JSDOM } from 'jsdom'
import fs from 'fs'
import path from 'path'
const html = fs.readFileSync(
path.resolve(__dirname, '../views/pages/index.ejs'),
'utf8'
)
let dom
let container
describe('Home page', () => {
beforeEach(() => {
dom = new JSDOM(html, { runScripts: 'dangerously' })
container = dom.window.document.body
})
test('should show 1', () => {
console.log('>>> ', container.querySelector('header'))
expect(container.querySelector('h1')).not.toBeNull()
expect(1).toBe(1)
})
})
But the issue is that when I querySelect something from container, it always returns null and I can't proceed in testing step. What am I doing wrong, and how can I get proper testing experience with .ejs composed markup files?
Here is my jest config file:
module.exports = {
clearMocks: true,
setupFilesAfterEnv: ['regenerator-runtime/runtime'],
testPathIgnorePatterns: ['/node_modules/'],
moduleFileExtensions: ['js', 'jsx', 'ejs', 'ejx'],
}