我有一个想要用ava和browser-env测试的函数
function foo () {
setTimeout(() => {
const event = new CustomEvent('pushcommand', { detail: 'foo', bubbles: true })
document.getElementById('command-history').dispatchEvent(event)
}, 1)
}我的测试代码是:
import test from 'ava'
import foo from 'foo.js'
test('foo', t => {
document.body.innerHTML = '<ul id="command-history"></ul>'
document.getElementById('command-history').addEventListener('pushcommand', event => {
t.is(event.detail, 'foo')
})
foo()
})但是我在ava中得到了错误:Error: Test finished without running any assertions。来自事件侦听器的代码被执行,只是ava在退出测试之前没有到达它。
有人知道怎么解决这个问题吗?
我尝试了test.serial,async await,t.end()都没有用。请帮帮忙。
发布于 2020-03-06 05:01:42
异步等待可能很棘手。在调用异步回调之前,测试可能已结束。由于没有返回任何承诺(异步),ava不知道要等到测试完成。像这样的东西应该有助于与ava沟通,以便等待承诺完成
import test from 'ava'
import foo from 'foo.js'
function foo () {
setTimeout(() => {
const event = new CustomEvent('pushcommand', { detail: 'foo', bubbles: true })
document.getElementById('command-history').dispatchEvent(event)
}, 1)
}
test('foo', async (t) => {
document.body.innerHTML = '<ul id="command-history"></ul>'
await new Promise((resolve, reject) => {
window.addEventListener('error', reject)
document.getElementById('command-history').addEventListener('pushcommand', event => {
t.is(event.detail, 'foo')
resolve()
})
foo()
})
})https://stackoverflow.com/questions/60553643
复制相似问题