HI WELCOME TO SIRIS

Node.js Assertion Testing

Leave a Comment
The Node.js Assert is the most elementary way to write tests. It provides no feedback when running your test unless one fails. The assert module provides a simple set of assertion tests that can be used to test invariants. 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 cannot be used as general purpose assertion library.

Node.js Assert Example

Let's see a simple example of Node.js Assert.
File: assert_example1.js
  1. var assert = require('assert');  
  2. function add (a, b) {  
  3.   return a + b;  
  4. }  
  5. var expected = add(1,2);  
  6. assert( expected === 3'one plus two is three');  
It will not provide any output because the case is true. If you want to see output, you need to make the test fail.
Node.js assert example 1
File: assert_example2.js
  1. var assert = require('assert');  
  2. function add (a, b) {  
  3.   return a + b;  
  4. }  
  5. var expected = add(1,2);  
  6. assert( expected === 4'one plus two is three');  
Now you will see the AssertionError.
Node.js assert example 2

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.