简体   繁体   中英

test for error thrown in node.js using mocha chai

I'm new to node.js, and I'm having problems setting up a simple unit test for a function I expect to throw an error. My function is very simple:

const which_min = function(array) {
   var lowest = 0;
   for (var i = 1; i < array.length; i++) {
      if (array[i] < array[lowest]) lowest = i;
   }
   return lowest;
}

I want to test if my function tosses an error when no argument is passed to it. In my test folder I have a test file

var assert = require('chai').assert;
var expect = require('chai').expect;
describe('#which_min()', function() {
context('with incorrect arguments', function() {
    it('errorTest', function() {
      expect(function(){utils.which_min();}).to.throw(new TypeError("Cannot read property 'length' of undefined"))
    })
  })
})

But I get what I find to be a quite peculiar error:

AssertionError: expected [Function] to throw 'TypeError: Cannot read property \'length\' of undefined' but 'TypeError: Cannot read property \'length\' of undefined' was thrown
  + expected - actual

I really do not see the difference in what I expect and what I get - so why do I not pass the test here? I expect it to be something with quotation marks?

Thanks /Kira

You are passing a new instance of TypeError to the expect() function, which means it will expect your which_min() function to throw that exact error instance (but it won't do that, it will throw another instance of the same error type with the same error message).

Try just passing the error string instead, so:

var assert = require('chai').assert;
var expect = require('chai').expect;
describe('#which_min()', function() {
context('with incorrect arguments', function() {
    it('errorTest', function() {
      expect(function(){utils.which_min();}).to.throw("Cannot read property 'length' of undefined")
    })
  })
})

In that case, Chai will expect any error type with the same error message to be thrown.

You could also choose to assert that the error is a TypeError like this:

var assert = require('chai').assert;
var expect = require('chai').expect;
describe('#which_min()', function() {
context('with incorrect arguments', function() {
    it('errorTest', function() {
      expect(function(){utils.which_min();}).to.throw(TypeError)
    })
  })
})

But then you aren't asserting that the error message is exactly what you expect.

Refer to the official Chai documentation here for more info: https://www.chaijs.com/api/bdd/#method_throw

EDIT:

As @Sree.Bh mentions, you can also pass both the expected type of the error and the expected error message to the throw() assertion, like so:

var assert = require('chai').assert;
var expect = require('chai').expect;
describe('#which_min()', function() {
context('with incorrect arguments', function() {
    it('errorTest', function() {
      expect(function(){utils.which_min();}).to.throw(TypeError, "Cannot read property 'length' of undefined")
    })
  })
})

Agree to @krisloekkegaard explanation why expect(function(){utils.which_min();}).to.throw(new TypeError("Cannot read property 'length' of undefined")) fails:

To check the 'error type' and the 'message', use :

expect(function () { utils.which_min(); })
.to.throw(TypeError, "Cannot read property 'length' of undefined");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM