node.jsunit-testingassertnodeunitchai

Assertions library for node.js?


Assertions provided by node.js assert for unit-testing are very limited. Even before I've written the first test I was already creating a few of assertions as it was clear I will keep re-using them.

Could you recommend some good library of assertions to test for common javascript situations (object structures, objects classes, etc., etc.)?

Ideally it should integrate well with nodeunit (or, better, extend it's assertions) - my assertions do not, I have to pass them test as an extra variable...

The only one I've seen is Chai. What could you say about it?


Solution

  • I use my very own assertion library, node-assertthat. It's specialty is its syntax which looks very fluent and (IMHO) is very readable (inspired by NUnit for .NET), e.g.:

    var actual = [...],
        expected = [...];
    
    assert.that(actual, is.equalTo(expected));
    

    Basically it works very well, but there are not too many asserts implemented yet. So whether it is "good" or not I won't decide - that's up to you.

    It makes use of a comparison library which provides things such as comparing objects by structure and some other nice things: compare.js.

    E.g., if you have to objects and you want to know if they are equal (by their values), you can do

    cmp.equal(foo, bar)
    

    or short as:

    cmp.eq(foo, bar)
    

    You can also compare objects by structure, e.g. check whether two objects implement the same interface. You could do this like

    cmp.equalByStructure(foo, bar)
    

    or short as:

    cmp.eqs(foo, bar);
    

    Again, I'll let you decide whether it's "good", but at least I am quite comfortable with using both.

    PS: I know that StackOverflow is no place to advertise your own projects, but I think that in this case the answer forces me to do this, as the answer to 'could you recommend' is 'my own tooling' in this case as for ME it is the best fit. Please don't consider this post as spam hence.