我有一个简单的 http Express 服务器,它服务于几个路由,比如 home、about、news 等。
对于每条路线,我使用res.render('some_ejs_file')
, 来呈现带有.ejs
扩展名的 html 页面。每个页面都有一个.js
文件脚本,我只是想知道如何渲染.ejs
文件以运行正确的 js 文件并渲染页面来测试它?
我安装了@testing-library
and jest
,这是我的home.test.js
文件:
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)
})
})
但问题是,当我来自 的querySelect
东西时container
,它总是返回null
,我无法继续进行测试步骤。我做错了什么,如何获得.ejs
组合标记文件的适当测试体验?
这是我的笑话配置文件:
module.exports = {
clearMocks: true,
setupFilesAfterEnv: ['regenerator-runtime/runtime'],
testPathIgnorePatterns: ['/node_modules/'],
moduleFileExtensions: ['js', 'jsx', 'ejs', 'ejx'],
}