108

I'm hoping to find some help with this problem. I'm trying to write tests for an application I am writing. I have distilled the problem in to the following sample code. I want to test that an error was thrown. I'm using Testacular as a test runner with mocha as the framework and chai as the assertion library. The tests run, but the test fails because an error was thrown! Any help is greatly appreciated!

function iThrowError() {
    throw new Error("Error thrown");
}

var assert = chai.assert,
    expect = chai.expect;
describe('The app', function() {
    describe('this feature', function() {
        it("is a function", function(){
            assert.throw(iThrowError(), Error, "Error thrown");
        });
    });
});
Louis
  • 146,715
  • 28
  • 274
  • 320
Chris Neitzer
  • 1,183
  • 2
  • 7
  • 7
  • 3
    Well, I figured out that if I change the assertion from `assert.throw(iThrowError(), Error, "Error thrown");` to `expect(iThrowError).to.throw();` This makes the test pass, but it does not seem to allow for checking if any specific error, which I think would be more useful. I think I am missing something still. – Chris Neitzer Feb 20 '13 at 15:01

3 Answers3

159

You're not passing your function to assert.throws() the right way.

The assert.throws() function expects a function as its first parameter. In your code, you are invoking iThrowError and passing its return value when calling assert.throws().

Basically, changing this:

assert.throws(iThrowError(), Error, "Error thrown");

to this:

assert.throws(iThrowError, Error, "Error thrown");

should solve your problem.

With args:

assert.throws(() => { iThrowError(args) }, Error);

or

assert.throws(function() { iThrowError(args) }, Error, /Error thrown/);
Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
red
  • 3,163
  • 1
  • 17
  • 16
  • 36
    What if I wanted to pass a parameter to the function? Is there a way to do that? – Glen Selle Feb 12 '14 at 17:26
  • 69
    Glen, to pass parameters to your function, try wrapping in a function: assert.throw(function() { iThrowError(args) }, Error) – Marc Gibbons Mar 26 '14 at 21:10
  • I had a similar problem using Chai expect, where I was actually calling the function rather than passing the reference. This solution fixed the problem, thanks! – jaseeey Oct 06 '16 at 22:55
  • @GlenSelle I used bind to do this. e.g. ``` function print(input){ console.log(input) }; var foo = print.bind(undefined,"my string"); foo() // prints "my string" ``` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind – JoeKir Nov 20 '16 at 20:47
  • 1
    You have a typo in your last 2 code sentences. It should be `assert.throws`. instead of `assert.throw` So the correct code would be: `assert.throws(() => { iThrowError(args) }, Error);` `assert.throws(function() { iThrowError(args) }, Error, /Error thrown/);` – Boogie Sep 18 '21 at 08:45
  • Fixed the above ^ – Steve Chambers Nov 12 '21 at 14:47
  • If you don't use Chai and just Mocha, change it to `assert.throws(() => iThrowError(args), { name: 'Error', message: 'Error thrown' });` Mocha does uses the [Node.js Assert library](https://nodejs.org/api/assert.html#assertthrowsfn-error-message). – adriaan Nov 29 '21 at 20:50
42

Adding to the top answer, if you need to invoke your function as part of the test (i.e. your function should only throw an error if certain parameters are passed), you can wrap your function call in an anonymous function, or, in ES6+, you can pass your function in an arrow function expression.

// Function invoked with parameter.
// TEST FAILS. DO NOT USE.
assert.throws(iThrowError(badParam), Error, "Error thrown"); // WRONG!

// Function invoked with parameter; wrapped in anonymous function for test.
// TEST PASSES.
assert.throws(function () { iThrowError(badParam) }, Error, "Error thrown");

// Function invoked with parameter, passed as predicate of ES6 arrow function.
// TEST PASSES.
assert.throws(() => iThrowError(badParam), Error, "Error thrown");

And, just for the sake of thoroughness, here's a more literal version:

// Explicit throw statement as parameter. (This isn't even valid JavaScript.)
// TEST SUITE WILL FAIL TO LOAD. DO NOT USE, EVER.
assert.throws(throw new Error("Error thrown"), Error, "Error thrown"); // VERY WRONG!

// Explicit throw statement wrapped in anonymous function.
// TEST PASSES.
assert.throws(function () { throw new Error("Error thrown") }, Error, "Error thrown");

// ES6 function. (You still need the brackets around the throw statement.)
// TEST PASSES.
assert.throws(() => { throw new Error("Error thrown") }, Error, "Error thrown");
Community
  • 1
  • 1
Jake
  • 4,829
  • 2
  • 33
  • 44
  • 4
    What's the point of repeating the accepted answer? – Louis Nov 07 '16 at 20:19
  • 8
    @Louis This is not a repeat. The accepted answer is to pass the function as a reference, and to **not** invoke the function. As I state at the top of my answer, I'm explaining how to actually invoke the function as part of the test if the test needs the invocation. – Jake Nov 08 '16 at 15:25
26

I saw you were able to resolve your problem but were not able to check for a specific error. To do so using Chai's expect/should syntax, you can use the parameters from the different signatures of throw():

@param{ ErrorConstructor } constructor
@param{ String | RegExp } expectederror message
@param{ String } message _optional_

In your example, you should be able to use either of the following:

expect(iThrowError).to.throw(/Error thrown/);
expect(iThrowError).to.throw(Error, /Error thrown/);
expect(iThrowError).to.throw(new Error('Error thrown'));

And (again, from chai's documentation), you could filter other error messages using:

expect(iThrowError).to.throw(Error).and.not.throw(/Another Error thrown/);

Hope this helps!

Vincent Simard
  • 524
  • 3
  • 8