Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js assert library vs. other assert libraries

Tags:

According to node.js assert library documentation:

The module is intended for internal use by Node.js, but can be used in application code via require('assert'). However, assert is not a testing framework, and is not intended to be used as a general purpose assertion library.

I was looking at Chai as an alternative assert library (no BDD API, only the assert API), and at the end I see that the assert functionality is very similar.

Why Chai's assert library is a better assert library? It does everything than node.js does (beside being just more rich in terms of assertion available, but that's just syntactic sugar-coating). Even simple things like the total count of assert executed is not available on both.

Am I missing something ?

like image 748
fabrizi0 Avatar asked Apr 05 '16 18:04

fabrizi0


People also ask

What are assertion libraries?

Assertion libraries are tools to verify that things are correct. This makes it a lot easier to test your code, so you don't have to do thousands of if statements. Example (using should.js and Node.js assert module): var output = mycode. doSomething(); output.

What is chai assertion library?

Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.

Why we use assert in node JS?

The assert module provides a way of testing expressions. If the expression evaluates to 0, or false, an assertion failure is being caused, and the program is terminated. This module was built to be used internally by Node.

What is assert deepEqual?

The assert. deepEqual() method tests if two objects, and their child objects, are equal, using the == operator. If the two objects are not equal, an assertion failure is being caused, and the program is terminated.


2 Answers

UPDATE (April 2017): Node.js no longer warns people away from using assert so the answer below is now outdated. Leaving it for historical interest, though.

Here's the answer I posted to a very similar question on the Node.js issue tracker.

https://github.com/nodejs/node/issues/4532 and other issues allude to the reason the documentation recommends against using assert for unit testing: There are edge case bugs (or at least certainly surprises) and missing features.

A little more context: Knowing what we now know, if we were designing/building Node.js core all over again, the assert module would either not exist in Node.js or else consist of far fewer functions--quite possibly just assert() (which is currently an alias for assert.ok()).

The reasons for this, at least from my perspective, are:

  • all the stuff being done in assert could easily be done in userland
  • core efforts are better spent elsewhere than perfecting a unit testing module that can be done in userland

There's additional context that others may choose to add here or not (such as why, all things being equal, we would favor keeping core small and doing things in userland). But that's the so-called 30,000 foot view.

Since assert has been in Node.js for a long time and a lot of the ecosystem depends on it, we are unlikely (at least as best as I can tell at the current time) to ever remove assert.throws() and friends. It would break too much stuff. But we can discourage people from using assert and encourage them to use userland modules that are maintained by people who care deeply about them and who aggressively fix edge-case bugs and who add cool new features when it makes sense. So that's what that's all about.

True, if you're doing straightforward assertions with simple cases, assert probably will meet your needs. But if you ever outgrow assert, you'll be better off with chai or whatever. So we encourage people to start there. It's better for them (usually) and better for us (usually).

I hope this is helpful and answers your question.

like image 157
Trott Avatar answered Sep 18 '22 22:09

Trott


I guess since nobody gave me any good feedback I'll try to provide some light on my original question after some time of working with both node.js assert and chai's assert.

The answer at the very end is that functionality-wise they are the same. The only reason why chai's assert exist is so if you read the code you can get a better understanding of the tests, but that's about it.

For example, testing for a null value with Node.js:

assert(foo === null);

And using chai:

assert.isNull(foo);

They are perfectly equivalent, and sticking to node.js assert limits your dependency list.

like image 25
fabrizi0 Avatar answered Sep 20 '22 22:09

fabrizi0