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

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

    This example only works with Node!

    When you use Mocha on Node.js you can use doesNotReject() or rejects() both require a function that returns a promise.


    Example for when it should reject:

    await rejects(testFunction());
    

    see: https://nodejs.org/api/assert.html#assert_assert_rejects_asyncfn_error_message

    Example for when it should not reject:

    await doesNotReject(testFunction());
    

    see: https://nodejs.org/api/assert.html#assert_assert_doesnotreject_asyncfn_error_message

提交回复
热议问题