Node.js Tutorial
- Node.js Tutorial
- Install Node.js on Windows
- Install Node.js on Linux/Ubuntu/CentOS
- Node.js First Example
- Node.js Console
- Node.js REPL
- Node.js Package Manager
- Node.js Command Line Options
- Node.js Global Objects
- Node.js OS
- Node.js Timer
- Node.js Errors
- Node.js DNS
- Node.js Net
- Node.js Crypto
- Node.js TLS/SSL
- Node.js Debugger
- Node.js Process
- Node.js Child Process
- Node.js Buffers
- Node.js Streams
- Node.js File System (FS)
- Node.js Path
- Node.js StringDecoder
- Node.js Query String
- Node.js ZLIB
- Node.js Assertion Testing
- Node.js V8
- Node.js Callbacks
- Node.js Events
- Node.js Punycode
- Node.js TTY
- Node.js Web Module
- NestJS
Node.js MySQL
Node.js MongoDB
Nodejs Difference
Node.js MCQ
Node.js Express
Nodejs Interview Questions
Node.js Assertion Testing
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
- var assert = require('assert');
- function add (a, b) {
- return a + b;
- }
- var expected = add(1,2);
- 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.
File: assert_example2.js
- var assert = require('assert');
- function add (a, b) {
- return a + b;
- }
- var expected = add(1,2);
- assert( expected === 4, 'one plus two is three');
Now you will see the AssertionError.