Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking all elements in an Array with Chai

When testing with Mocha and Chai, I often need to test whether all the elements in an array satisfy a condition.

Currently I'm using something like the following:

var predicate = function (el) {
  return el instanceof Number;
};

it('Should be an array of numbers', function () {
  var success, a = [1, 2, 3];
  success = a.every(predicate);
  expect(success).to.equal(true);
});

Looking through the docs, I can't immediately see anything which provides this kind of behavior. Am I missing something or will I have to write a plugin to extend chai?

like image 730
StickyCube Avatar asked Jul 21 '15 12:07

StickyCube


1 Answers

Might not be a big improvement over your current approach, but you could do something like:

expect(a).to.satisfy(function(nums) { 
    return nums.every(function(num) {
        return num instanceof Number;
    }); 
});
like image 155
Stephen Thomas Avatar answered Sep 24 '22 22:09

Stephen Thomas