背景
因此,我一直在学习一篇教程,以了解JS测试框架,并了解到要对Web进行测试,最好使用JSDOM库,即它不需要浏览器并带来DOM,然后可以在无头时装中执行它。
对于断言库,我使用的是Chai,作为测试框架,我选择了Mocha
测试用例:我希望通过读取它的h1标记的值来测试html文件,在本例中是Hello,我将断言它为true。
import {expect} from 'chai'
import jsdom from 'jsdom'
import fs from 'fs'
describe('index.html', () => { // eslint-disable-line
it('should say hello' , () => { // eslint-disable-line
const index = fs.readFileSync('./src/index.html', 'utf-8')
jsdom.env(index, function (err, window) { // eslint-disable-line
const h1 = window.document.getElementsByTagName('h1')[0]
expect(h1.innerHTML).to.equal('Hello World!')
window.close()
})
})
})对于较早版本的 JSDOM 包来说,这很好,但正如我检查了JSDOM文档(即https://github.com/jsdom/jsdom ),新的方法是JSDOM构造函数。
因此,我如何迁移这个测试用例以坚持JSDOM库的更新样式。
发布于 2018-08-17 11:00:40
不知何故,我设法使我的测试用例得以建立并运行。
describe('index.html', () => { // eslint-disable-line
it('should say hello' , (done) => { // eslint-disable-line
const options = { }
JSDOM.fromFile('./src/index.html', options).then(dom => {
const h1 = dom.window.document.getElementsByTagName('h1')[0]
expect(h1.innerHTML).to.equal('Hello World!')
done()
}).catch(done)
})
})https://stackoverflow.com/questions/51893274
复制相似问题