Last Updated: March 10, 2018
·
3.145K
· kanshuYokoo

AssertionError: Missing expected exception. node.js assertion

When I use the assert.throws of the node.js assert class with mocha to do unit test on javascript, I got the error message saying 'AssertionError: Missing expected exception'.
this is because assert.throws dose not handle Error thrown by an async function. so I have to catch the error in the test code and assert the result instead of using assert.throw.
for example,

myFunctionTobeChecked(Object) should take a valid Object as an argument.

it('myFunctionTobeChecked(Object)', function() {
      myFunctionTobeChecked({}).then(
        function() {throw new Error('Test failed: myFunctionTobeChecked({}) should be failed when empty Object was given');},
        function(error) {assert(error, error.message);}
    );
});   

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside