O'Reilly logo

Mastering Node.js - Second Edition by Kevin Faaborg, Sandro Pasquali

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The assert module

Node's assert module is used for simple unit testing. In many cases, it suffices as a basic scaffolding for tests, or is used as the assertion library for testing frameworks (such as Mocha, as we'll see later). Usage is straightforward: we want to assert the truth of something, and throw an error if our assertion is not true. Consider this example:

> require('assert').equal(1,2,'Not equal!')AssertionError [ERR_ASSERTION]: Not equal!>

If the assertion were true (both values are equal), nothing would be returned:

> require('assert').equal(1,1,"Not equal!")undefined

Following the UNIX Rule of Silence (when a program has nothing surprising, interesting, or useful to say, it should say nothing), assertions only return a value ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required