assert
- Strict judgment keyword
strict
{#strict} - Methods provided by the assertion library
- assert(value: any, message?: string|Error)
- assert.ok(value: any, message?: string|Error)
- assert.equal(actual, expected: any, message?: string|Error)
- assert.deepEqual(actual, expected: any, message?: string|Error)
- assert.notEqual(actual, expected: any, message?: string|Error)
- assert.notDeepEqual(actual, expected: any, message?: string|Error)
A library for adding checkpoints in scripts - the assertion library assert
, is one of the built-in libraries in Node.js. It can be imported by adding the following script to the head of the script:
const assert = require('assert');
Or directly drag and drop from the toolbox to the script editor to automatically generate scripts.
Strict judgment keyword strict
For all methods with the equal
keyword, such as equal()
, deepEqual()
, notEqual()
, notDeepEqual()
, etc., you can change equal
to strictEqual
for Strict judgment, such as changing to strictEqual()
, deepstrictEqual()
, notstrictEqual()
, notDeepstrictEqual()
.
By default, abstract (non-strict) judgment is used, and the strict
keyword can be changed to strict judgment. In general, if you do not plan to learn the relationship between types in the JavaScript language, you can Ensure that the test results are sufficiently reliable. The difference between the two is the difference between ==
judgment and ===
judgment. The relationship reflected in the script is as follows:
Strictly equal
let num = 0;
let obj = new String("0");
let str = "0";
let b = false;
console.log(num === num); // true
console.log(obj === obj); // true
console.log(str === str); // true
console.log(num === obj); // false
console.log(num === str); // false
console.log(obj === str); // false
console.log(null === undefined); // false
console.log(obj === null); // false
console.log(obj === undefined); // false
abstract equality
let num = 0;
let obj = new String("0");
let str = "0";
let b = false;
console.log(num == num); // true
console.log(obj == obj); // true
console.log(str == str); // true
console.log(num == obj); // true
console.log(num == str); // true
console.log(obj == str); // true
console.log(null == undefined); // true
// both false, except in rare cases
console.log(obj == null);
console.log(obj == undefined);
Methods provided by the assertion library
assert(value: any, message?: string|Error)
Determine whether the incoming parameter is true, or throw an error message.
- value: Any type, check if it is true input, so you can pass in an expression, such as
assert(a===b, 'a not equal to b')
. - message:
string
orError
type.
assert.ok()
for easy usage.
assert.ok(value: any, message?: string|Error)
- value: any type, check if it is true input, so you can pass in an expression, such as
assert.ok(a===b, 'a not equal to b')
. - message:
string
orError
type.
assert.equal(actual, expected: any, message?: string|Error)
Used to determine whether the expected value is equal to the actual value. The equality relation is equivalent to the abstract equality ==
operator. If you add the strict
keyword and call assert.strictEqual()
instead, it is equivalent to the strict equality ===
operator.
- actual: any type,
- expected: any type,
- message:
string
orError
type.
const assert = require('assert');
assert.equal(1, 1);
// OK, 1 == 1
assert.equal(1, '1');
// OK, 1 == '1'
assert.equal(NaN, NaN);
// OK
assert.equal(1, 2);
// AssertionError: 1 == 2
assert.equal({ a: { b: 1 } }, { a: { b: 1 } });
// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }
assert.deepEqual(actual, expected: any, message?: string|Error)
It is used to judge whether the expected value is equal to the actual value depth, and is usually used to judge whether two objects are completely consistent from the outside to the inside. Due to the overhead of judging object equality, object judgment in JavaScript only judges whether the source of the object (memory location, or understood as a pointer) is consistent.
- actual: any type,
- expected: any type,
- message:
string
orError
type
For object judgment, as long as the source of the object is inconsistent, even two empty objects are not equal:
let a = {};
let b = {};
console.log(a == b); // false
b = a; // Point b to the memory address of a
console.log(a == b); // true
In-Depth Comparison Details
"Deep" equality means that enumerable "own" properties of subobjects are also compared:
const assert = require('assert');
const obj1 = {
a: {
b: 1
}
};
const obj2 = {
a: {
b: 2
}
};
const obj3 = {
a: {
b: 1
}
};
// Construct a new instance with the class of obj1, here is an empty object {}
const obj4 = Object.create(obj1);
assert.deepEqual(obj1, obj1);
// OK
// The values of b are different:
assert.deepEqual(obj1, obj2);
// throw AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }
assert.deepEqual(obj1, obj3);
// OK
// The same type does not work:
assert.deepEqual(obj1, obj4);
// throw AssertionError: { a: { b: 1 } } deepEqual {}
If the values are not equal, an [AssertionError
] is thrown and the message
attribute is set equal to the value of the message
parameter.
If the message
parameter is not defined, a default error message will be assigned.
If the message
parameter is an instance of [Error
] then it will be thrown instead of [AssertionError
].
assert.notEqual(actual, expected: any, message?: string|Error)
Used to determine whether the expected value is not equal to the actual value. The equality relation is equivalent to the abstract inequality !=
operator. If you add the strict
keyword and call assert.notStrictEqual()
instead, it is equivalent to the strict inequality !==
operator.
- actual: any type,
- expected: any type,
- message:
string
orError
type.
assert.notDeepEqual(actual, expected: any, message?: string|Error)
It is used to judge whether the expected value and the actual value are not equal in depth. It is usually used to judge whether there is an inconsistency between the external and internal attributes of two objects.
- actual: any type,
- expected: any type,
- message:
string
orError
type