Expectjs with underscore support.
expect([1,2,3,4]).to._.find(function(n) {
return n % 2 == 0;
});
expectjs is a 'Minimalistic BDD assertion toolkit', which is very simple to use, but is short in matchers that makes writing tests less jolly.
This extension inserts underscore functions as matchers under the belt, so you can easily test with power of underscore in your hands.
$ npm install expect.underscore.js
var expect = require('expect.js'),
_ = require('underscore');
// PATCH expect here
expect = require('expect.underscore.js').extend(expect, _);
describe('check users for fred', function() {
var userNames = [
'barney',
'fred',
'pebbles',
];
var users = [
{ 'user': 'barney', 'active': true },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': false }
];
it('using expectjs', function() {
// you either check with the whole object,
expect(userNames).to.contain('fred');
// or split the lines
var fred = _.find(users, {user: 'fred'});
expect(fred).to.be.ok();
});
it('using _.find', function() {
// better readability
expect(users).to._.find({user: 'fred'});
});
describe('.not.to', function() {
it('using _.find', function() {
expect(users).not.to._.find({
active: undefined
});
});
});
});
You can use every underscore methods under the .to._
object, and it will check if the result is .ok()
.
To check the full methods supported, check the underscore document.
If you prefer lodash, you can pass lodash as an argument instead.
Currently tested with lodash (sorry), and only in NodeJS.