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 ...

Get Mastering Node.js - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.