Home >>Nodejs Tutorial >Node.js Assertion Testing
The Node.js assert is the most common way of writing tests. This does not offer feedback if the test runs when one fails. The module assert provides a basic set of assertion checks which can be used to evaluate invariants. The module is intended for internal use by Node.js, but can be used via require ('assert') in the application code.
However, asserting is not a testing framework and can not be used as a library for assertion of general purpose.
Let's take a simple example of Assert Node.js
File: assert_example1.js
var assert = require('assert');
function add (x, y)
{
return x + y;
}
var expected = add(5,3);
assert( expected === 8, 'five plus three is eight');
It will not yield any output, as the case is true. If you wish to see the output, the test must fail.
File: assert_example2.jsvar assert = require('assert'); function add (x, y) { return x + y; } var expected = add(5,3); assert( expected === 9, 'five plus three is eight');
You are going to see the AssertionError now.