YouTip LogoYouTip

Nodejs Assert Module

[![Image 1: Java File](#)Node.js Built-in Modules](#) * * * The `assert` module in Node.js is a built-in utility library for writing unit tests. It provides a series of assertion functions to verify whether the behavior of your code matches expectations. If an assertion fails, the `assert` module throws an `AssertionError` exception. The `assert` module is very suitable for use during development and testing phases, helping developers quickly identify issues in their code. * * * ## How to Use the assert Module? To use the `assert` module, you first need to import it: const assert = require('assert'); ### Basic Assertion Methods #### 1. assert.ok(value[, message]) `assert.ok()` is the most basic assertion method. It tests whether `value` is a truthy value. If `value` is a falsy value, it throws an `AssertionError`. ## Example assert.ok(true);// Pass assert.ok(1);// Pass assert.ok(false);// Throws AssertionError assert.ok(0,'value is falsy');// Throws AssertionError: value is falsy #### 2. assert.equal(actual, expected[, message]) `assert.equal()` uses the `==` operator to compare whether `actual` and `expected` are equal (loose equality). ## Example assert.equal(1,1);// Pass assert.equal(1,'1');// Pass assert.equal(1,2);// Throws AssertionError #### 3. assert.strictEqual(actual, expected[, message]) `assert.strictEqual()` uses the `===` operator to compare whether `actual` and `expected` are strictly equal. ## Example assert.strictEqual(1,1);// Pass assert.strictEqual(1,'1');// Throws AssertionError #### 4. assert.deepEqual(actual, expected[, message]) `assert.deepEqual()` is used to compare whether the contents of two objects or arrays are equal (recursive comparison). ## Example assert.deepEqual({ a:1},{ a:1});// Pass assert.deepEqual([1,2],[1,2]);// Pass assert.deepEqual({ a:1},{ a:'1'});// Pass (loose comparison) #### 5. assert.deepStrictEqual(actual, expected[, message]) `assert.deepStrictEqual()` is similar to `deepEqual`, but uses strict comparison (`===`). ## Example assert.deepStrictEqual({ a:1},{ a:1});// Pass assert.deepStrictEqual({ a:1},{ a:'1'});// Throws AssertionError * * * ## Other Common Assertion Methods ### 1. assert.notEqual(actual, expected[, message]) Tests whether `actual` and `expected` are not equal (`!=`). ## Example assert.notEqual(1,2);// Pass assert.notEqual(1,'1');// Throws AssertionError ### 2. assert.notStrictEqual(actual, expected[, message]) Tests whether `actual` and `expected` are not strictly equal (`!==`). ## Example assert.notStrictEqual(1,'1');// Pass assert.notStrictEqual(1,1);// Throws AssertionError ### 3. assert.throws(block[, error][, message]) Tests whether the `block` function will throw an error. ## Example assert.throws( ()=>{ throw new Error('Wrong value'); }, Error );// Pass assert.throws( ()=>{ throw new Error('Wrong value'); }, /Wrong/ );// Pass (regex matching error message) ### 4. assert.doesNotThrow(block[, error][, message]) Tests whether the `block` function will not throw an error. ## Example assert.doesNotThrow( ()=>{ const x =1+1; }, Error );// Pass ### 5. assert.fail() Forcibly throws an `AssertionError`. ## Example assert.fail('Test failed');// Throws AssertionError: Test failed * * * ## Practical Application Example Suppose we have a simple function `add` that calculates the sum of two numbers: ## Example function add(a, b){ return a + b; } We can use the `assert` module to write tests for it: ## Example const assert = require('assert'); // Test normal cases assert.strictEqual(add(1,2),3,'1 + 2 should be 3'); assert.strictEqual(add(-1,1),0,'-1 + 1 should be 0'); // Test edge cases assert.strictEqual(add(0,0),0,'0 + 0 should be 0'); assert.strictEqual(add(1.5,2.5),4,'1.5 + 2.5 should be 4'); // Test error cases assert.throws( ()=> add('1',2), TypeError, 'Adding string to number should throw TypeError' ); * * * ## Notes 1. **Production Environment**: The `assert` module is mainly used for development and testing environments. It is not recommended to use it in production environments because assertion failures will cause the program to crash. 2. **Strict Mode**: Node.js also provides the `assert/strict` module, which is a strict version of the `assert` module. All comparisons use strict equality (`===`). const assert = require('assert/strict'); 3. **Custom Error Messages**: Providing clear error messages for each assertion helps quickly locate problems. 4. **Performance Considerations**: Although the `assert` module is convenient, it should be used cautiously in performance-critical code because assertion checks add extra overhead. * * * ## Summary The `assert` module is a simple but powerful testing tool in Node.js, especially suitable for unit testing and code verification during the development phase. By reasonably using various assertion methods, you can effectively ensure the correctness of your code. Remember that after completing development, you should replace the `assert` module with more professional testing frameworks (such as Mocha, Jest, etc.) for more comprehensive testing. [![Image 2: Java File](#)Node.js Built-in Modules](#)
← Nodejs Console ModuleNodejs Repl Module β†’