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

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

    This is my Solution for the problem .

        try {
            // here the function that i expect to will return an errror
            let walletid = await Network.submitTransaction(transaction)
        } catch (error) {
            //  assign error.message to ErrorMessage
            var ErrorMessage = error.message;
            //  catch it and  re throw it in assret.throws fn and pass the error.message as argument and assert it is the same message expected
            assert.throws(() => { throw new Error(ErrorMessage) },'This user already exists');
        }
        // here assert that ErrorMessage is Defined ; if it is not defined it means that no error occurs
        assert.isDefined(ErrorMessage);
    

提交回复
热议问题