Как я могу утверждать для асинхронного `throw`? - PullRequest
0 голосов
/ 28 сентября 2018

Я хотел бы заявить, что мой код не генерирует никаких возможных ошибок.

Проблема в том, что мой код выполняет операции 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://how -to-assert-async-throw.glitch.me /

Код aboe проходит тест, даже если ошибка в конечном итоге будет выдана.

1 Ответ

0 голосов
/ 28 сентября 2018

Попробуйте и дайте мне знать, если это поможет.

    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();
   });
...