gpt4 book ai didi

node.js - Jsdom.env 到新的 JSDOM ,迁移

转载 作者:太空宇宙 更新时间:2023-11-04 00:06:49 25 4
gpt4 key购买 nike

Background

因此,我一直在遵循教程来了解 JS 测试框架,并了解到要测试 Web UI,最好使用 JSDOM 库,因为它不需要浏览器并带来 DOM,然后可以以 headless 方式执行。

对于断言库,我使用 Chai 并选择 Mocha 作为测试框架

Test Case : I would like to test an html file by reading its h1 tag's value i.e. Hello World in this case and I would assert it to 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 库的更新风格。

最佳答案

不知何故,我成功地启动并运行了我的测试用例。

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)
})
})

关于node.js - Jsdom.env 到新的 JSDOM ,迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51893274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com