我具有要用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()
无济于事。请帮助。
异步等待可能很棘手。测试可能在调用异步回调之前结束。因为没有返回承诺(异步),所以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()
})
})