May 10, 2021 Node.js
Node.js s assert module is primarily used when writing unit tests for programs, and assertions allow errors to be detected and troubleshooted early.
稳定性: 5 - 锁定
This module can be used for unit testing in
require('assert')
Use the parameters operator to test whether the parameters
actual
expected
and expected (expected) are equal.
Test whether
value
is
true
this function and
assert.equal(true, !!value, message);
Equivalent.
Determines whether the
actual
real and whether the
expected
expected value is equal.
Determines whether the
actual
and the
expected
are not equal.
Perform a depth comparison to determine whether the
actual
and
expected
are equal.
The depth compares whether the two parameters are not equal.
The depth compares whether the two parameters are equal.
This function uses the operator '!'' to strictly compare whether the two parameters are not equal.
Declares a
block
throw an
error
can
error
a constructor, regular expression, or other validator.
Use constructors to validate instances:
assert.throws(
function() {
throw new Error("Wrong value");
},
Error
);
Use regular expressions to verify error messages:
assert.throws(
function() {
throw new Error("Wrong value");
},
/value/
);
User-defined error validator:
assert.throws(
function() {
throw new Error("Wrong value");
},
function(err) {
if ( (err instanceof Error) && /value/.test(err) ) {
return true;
}
},
"unexpected error"
);
Declaring
block
does not throw an error, see
assert.throws
Determines whether the parameter value is false and, if true, throws an exception. Typically used to test the first parameter in the callback, error.