Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert an error is thrown using the new to execute a constructor function [duplicate]

Using Chai with Mocha, what syntax would I use to assert that an error is thrown when using the new keyword to execute a constructor function? I get an error when I use the following:

assert.throw(new SomeFunction, Error);

This returns:

AssertionError: expected { Object () } to be a function

like image 902
Spencer Carnage Avatar asked Dec 26 '22 16:12

Spencer Carnage


1 Answers

Pass a function to assert.throw:

assert.throw(function () {
    new SomeFunction()
}, Error);

The reason what you had did not work is that the new SomeFunction is interpreted as new SomeFunction() and executed before assert.throw executes. So you end up running assert.throw with an object which is an instance of SomeFunction, rather than with a function that instantiates an object.

like image 109
Louis Avatar answered Dec 28 '22 07:12

Louis