jeudi 25 juin 2020

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

Aucun commentaire:

Enregistrer un commentaire