Node.js assert Module
In Node.js, the assert module is a built-in utility library for writing unit tests. It provides a series of assertion functions to verify that the behavior of the code meets expectations. If an assertion fails,assert the module will throw an AssertionError exception.
assert The module is very suitable for use during development and testing, helping developers quickly find problems in the code.
How to use the assert module?
To use 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, 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() Use == Operator Comparison actual and expected whether they 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() Use === Operator Comparison actual and expected whether they are strictly equal.
Example
assert.
strictEqual
(
1
,
1
)
;
// Pass
assert.
strictEqual
(
1
,
'1'
)
;
// Throws AssertionError
4. assert.deepEqual(actual, expected[, message])
assert.deepEqual() 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() Similar to deepEqual, but use 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])
Testing actual and expected whether they are not equal (!=)。
Example
assert.
notEqual
(
1
,
2
)
;
// Pass
assert.
notEqual
(
1
,
'1'
)
;
// Throws AssertionError
2. assert.notStrictEqual(actual, expected[, message])
Testing actual and expected whether they are not strictly equal (!==)。
Example
assert.
notStrictEqual
(
1
,
'1'
)
;
// Pass
assert.
notStrictEqual
(
1
,
1
)
;
// Throws AssertionError
3. assert.throws(block[, error][, message])
Testing block whether a function throws an error.
Example
assert.
throws
(
(
)
=>
{
throw
new
Error
(
'Wrong value'
)
;
}
,
Error
)
;
// Pass
assert.
throws
(
(
)
=>
{
throw
new
Error
(
'Wrong value'
)
;
}
,
/Wrong/
)
;
// Pass (regex matches the error message)
4. assert.doesNotThrow(block[, error][, message])
Testing block whether a function does not throw an error.
Example
assert.
doesNotThrow
(
(
)
=>
{
const
x
=
1
1
;
}
,
Error
)
;
// Pass
5. assert.fail([message])
Forcefully throw an AssertionError。
Example
assert.
fail
(
'Test failed'
)
;
// Throws AssertionError: Test failed
Practical Application Examples
Suppose we have a simple function addthat 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 the normal case
assert.
strictEqual
(
add
(
1
,
2
)
,
3
,
'1 + 2 should be 3'
)
;
assert.
strictEqual
(
add
(
1
,
1
)
,
0
,
'-1 + 1 should be 0'
)
;
// Test boundary 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 the error case
assert.
throws
(
(
)
=>
add
(
'1'
,
2
)
,
TypeError
,
'Adding string to number should throw TypeError'
)
;
Notes
-
Production environment:
assertThe module is mainly used in development and testing environments and is not recommended for production, because an assertion failure will cause the program to crash. -
Strict mode: Node.js also provides the
assert/strictmodule, which is theassertstrict version of the module, where all comparisons use strict equality (===)。
const assert = require('assert/strict'); 3.
Custom Error Messages: Providing a clear error message for each assertion helps to quickly locate problems.
4.Performance considerations: Although assert The module is convenient, but should be used cautiously in performance-critical code, because assertion checks add extra overhead.
Summary
assert The 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 the various assertion methods, you can effectively ensure the correctness of your code. Remember that after completing development, you should replace it with a more professional testing framework (such as Mocha, Jest, etc.) assert module for more comprehensive testing.
Node.js Built-in Modules