This blog is alive at last.
So, did you ever want to assert the content out of an array in your mocha tests?
myPosts = [{ header: 'Hello', text: 'The story of my life', category: 'Random blah' }, { header: 'Next phase', text: 'The plan' } ];
You probably did something like this to make sure all your posts has a header:
myPosts.forEach(function(post) {
expect(post.header).to.not.be.undefined;
});
And maybe you wrote this to make sure at least one of them was in the category 'Randon blah'?
var foundIt = false;
myPosts.forEach(function(post) {
if (post.category === 'Random blah')
foundIt = true;
});
expect(foundIt).to.be.true;
Well that works, but it's ugly.
Why bother? Personally I hate seeing code like that one above. Besides from knowing it could be prettier it makes for a slow read and the next person reading it is probably a collegue who broke your test and is close to handing it back to you if it's not clear what to expect of it. Make yourself happy, now and later, by making it so easy to read that you won't have to explain or get new glasses next time you break the code.
How? Try this very neat plugin that I found a few days ago. Install it, require it and then go
myPosts.should.all.have.a.property("header");
and
myPosts.should.contain.an.item.with.property("category", "Random blah");
Readable, don't you think?