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

后端 未结 11 893
花落未央
花落未央 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:46

    1. Install chai-as-promised for chai (npm i chai-as-promised -D)
    2. Just call your promise, no await should be applied!
    import chai from 'chai';
    import chaiAsPromised from 'chai-as-promised';
    
    chai.use(chaiAsPromised);
    
    const expect = chai.expect;
    
    describe('MY_DESCR', () => {
      it('MY_TEST', async () => {
        expect(myAsyncFunctionThatWillReject()).to.eventually.be.rejected; 
      });
    });
    

提交回复
热议问题