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?
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;
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With