Now that you have a place for testing your code, you need some way of verifying that your code runs as expected. For this, you need an assertion testing library.
There are many assertion testing libraries for many programming styles, but here we're going to use the one that already comes bundled with Node.js, the assert
module. It contains the smallest set of utility functions you need to describe what expectations you have for each test. At the top of each testing file, you need the assertion library using require
:
var assert = require('assert');
Note
You can assert the "truthiness" of any expression. "Truthy" and "falsy" are concepts in JavaScript (and other languages), where type coercion allows certain values to equate to Boolean true or false, respectively. Some examples are as follows:
var a = true; assert.ok(a, 'a should be truthy');
The falsy values are:
false
null
undefined
the empty string
0
(the number zero)NaN
All other values are truthy.
You can also test for equality...