Question

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?

Était-ce utile?

La solution 2

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.

Autres conseils

It's also somewhat a matter of preference — whether you prefer to test with the assert syntax or BDD-style assertions (smth.must.equal(...)).

For the assert style, Chai's assert may work well. It has more built-in matchers that Node's own assert module.

If you find the BDD-style more readable and fluent, all three do that:

They differ primarily by the simplicity or complexity of their API when it comes to various matchers. Their essential equality assertions, though, are interchangable — foo.must.equal(42) or foo.should.equal(42).

There's one thing you need to be aware when picking Chai.js and Should.js that I argue is a fundamental design mistake — their practice of asserting on property access as opposed to calling the matcher as a function. I've written a critique of asserting on property access and how it may cause false positives in tests.

Chai is great. I’ve tried quite a few different setups for both Node and browser testing but the only one that satisfies me is Mocha + Chai + Sinon. But choosing a assertion library is also a matter of style, I personaly like chai.expect with it’s chained API, and it has pretty must every methods you need : type validation, object property checking, exceptions… I also find it very flexible.

You might be interested in Hamjest, a JavaScript matcher library based on Hamcrest.

I provides a framework agnostic library of assertions and matchers that can be used with nodeunit, mocha, jasmin and others.

It has two main advantages over Chai, Jasmin and similar frameworks:

  1. Matchers can be nested and combined to create very expressive assertions.
  2. Assertion errors describe the reason for the mismatch in great detail (e.g. which property did not match, which element was missing, etc.) instead of just repeating the assertion.

Disclaimer: I'm the main author of Hamjest.

Expect is a easy-to-use extendible assertion library for NodeJS and the browser. I have used it a couple times with Mocha and I can say it has any assertion you need. You can learn how to use it here. Example:

var pi = Math.PI;
expect(pi)
  .toExist()
  .toBeLessThan(4)
  .toBeGreaterThan(3);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top