Verify that an exception is thrown using Mocha / Chai and async/await

后端 未结 11 768
花落未央
花落未央 2020-12-14 05:50

I\'m struggling to work out the best way to verify that a promise is rejected in a Mocha test while using async/await.

Here\'s an example that works, but I dislike t

11条回答
  •  时光说笑
    2020-12-14 06:47

    You can use async/await and should for simple verification

    it('should not throw an error', async () => {
      try {
        let r = await wins();
        r.should.equal('Winner');
      } catch (error) {
        error.should.be.null(); //should.not.exist(error) can also be used
      }
    });
    
    it('throws an error', async () => {
      try {
        await fails();
      } catch (error) {
        error.should.be.Error();
        error.should.have.value("message", "Contrived Error");
      }
    });
    

提交回复
热议问题