Как я могу утверждать для асинхронного `throw`?
Я хотел бы заявить, что мой код не генерирует никаких возможных ошибок.
Проблема в том, что мой код выполняет операции DOM, которые вызывают асинхронные реакции:
const slot = document.createElement('slot');
myElement.attachShadow({mode:'open'}).appendChild(slot);
// ...
slot.addEventListener('slotchange', function badFunction(){
throw "MyError";
// const observer = new MutationObserver(()=>{});
// observer.observe(null);
});
// ...
it('when input child element is removed, should not throw an error', function() {
function disconnect(){
// this will throw once slotchange is emmited
myElement.removeChild(myElement.firstElementChild);
}
expect(disconnect).not.to.throw();
});
- рабочий фрагмент: https://glitch.com/edit/?
- живой пример: путь = index.html: 28: 9
https://how-to-assert-async-throw.glitch.me/
Код aboe проходит тест, даже если ошибка в конечном итоге будет выдана.
1 ответ
Попробуйте это и дайте мне знать, если это поможет.
const slot = document.createElement('slot');
myElement.attachShadow({mode:'open'}).appendChild(slot);
// ...
slot.addEventListener('slotchange', function badFunction(){
throw "MyError";
// const observer = new MutationObserver(()=>{});
// observer.observe(null);
});
// ...
it('when input child element is removed, should not throw an error', async function() {
async function disconnect(){
// this will throw once slotchange is emmited
await myElement.removeChild(myElement.firstElementChild);
}
expect(await disconnect()).not.to.throw();
});