The Assert module is a built-in module for node, which is used primarily for determination. If the expression does not meet expectations, an error is thrown. The module provides 11 methods, but only a few are commonly used.
1.assert () Assert (value[, message])
assert.ok()
Alias, OK is another name for the Assert method, exactly the same as the Assert method.
The Assert method accepts two parameters, and when the first parameter corresponds to a Boolean value of true, there is no hint that returns undefined. When the first parameter corresponds to a Boolean value of False, an error is thrown, and the prompt for the error is the string of the second parameter setting
// format assert (value, message) // Example var assert = require (' assert '); function Add (A, b) { return a + b;} var expected = Add (= = = 3, ' expected 1 plus 2 equals 3 ');
The above code will not have any output, because the first parameter of the Assert method is true.
ASSERT (expected = = = 4, ' expected 1 plus 2 equals 3 ')// Assertionerror: expected 1 plus 2 equals 3
The above code throws an error because the first argument of the Assert method is false.
2.assert.deepequal (Actual, expected[, message])
actual
the test parameter expected
is equal to the parameter depth. The original value is compared using the equality operator ( ==
). (The popular explanation deepequal method is used to compare two objects.) As long as their property one by one corresponds, and the values are equal, the two objects are considered equal, or an error is thrown. )
Tests only the enumerable properties of itself, without testing the object's prototype, connector, or non-enumerable properties (these cases are used assert.deepStrictEqual()
).
1 //format2 Assert.deepequal (actual, expected, [message])3 4 //Example5 varassert = require (' Assert ');6 7 varList1 = [1, 2, 3, 4, 5];8 varList2 = [1, 2, 3, 4, 5];9 TenAssert.deepequal (List1, List2, ' expected two arrays should have the same attribute '); OneExpected two arrays should have the same properties A varPerson1 = {"Name": "John", "Age": "21" }; - varPerson2 = {"Name": "John", "Age": "21" }; - theAssert.deepequal (Person1, Person2, ' expected two objects should have the same attribute ');
Expected two arrays should have the same properties
Map
And Set
The included children are also tested.
The enumerable properties of a child object are also tested:
1Onst assert = require (' Assert ');2 3Const OBJ1 = {4 A: {5B:16 }7 };8Const OBJ2 = {9 A: {TenB:2 One } A }; -Const OBJ3 = { - A: { theB:1 - } - }; -Const OBJ4 =object.create (obj1); + - assert.deepequal (obj1, obj1); + //the test passes, and the object is equal to itself. A at assert.deepequal (obj1, obj2); - //throw Assertionerror: {A: {b:1}} deepequal {A: {b:2}} - //because the value of the B property is different. - - assert.deepequal (obj1, obj3); - //the test passes, and two objects are equal. in - assert.deepequal (obj1, obj4); to //throw Assertionerror: {A: {b:1}} deepequal {} + //because the prototype is not tested.
View Code
Nodejs Assert module