首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何断言异步“`throw`”?

如何断言异步“`throw`”?
EN

Stack Overflow用户
提问于 2018-09-28 16:51:28
回答 1查看 40关注 0票数 0

我想断言,我的代码不会引发任何最终的错误。

问题是:我的代码执行DOM操作,这会触发异步反应:

代码语言:javascript
复制
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://how-to-assert-async-throw.glitch.me/

上面的代码通过了测试,即使最终会抛出错误。

EN

回答 1

Stack Overflow用户

发布于 2018-09-28 17:28:25

试试这个,让我知道这是否有效。

代码语言:javascript
复制
    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();
   });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52559691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档