First, the Assert Assert module is introduced
var assert = require (' assert ');
1.assert (value, message), Assert.ok (value, [message])//Determines whether the median value is true
Assert.iferror (value)//test value is not false and is thrown when true. Often used for checking the first error parameter in a callback.
var a = 0; assert (A,' This requires a value of true '); // Assertionerror: This requires a value of trueAssert.ok (A, ' here also to be true '); // Assertionerror: This is also true
2. Assert.fail (actual, expected, message, operator)//throws an exception, message is displayed when message is not used, operator as delimiter
var a = 0; Assert.fail (A,// assertionerror:a! =1assert.fail (a,2, ' < '); // assertionerror:0 < 2
3.assert.equal (actual, expected, [message])//("= =" judgment) is equal to the opposite: notequal
Assert.strictequal (actual, expected, [message])//("= = =" judgment) is equal to the opposite: notstrictequal
var a = 2; var a= ' 2 '; Assert.equal (A, B,' b ' unequal = = ') assert.notequal (A, B,' equal ') // Assertionerror:a,b equal assert.strictequal (A, B, ' A, b not exactly equal = = = ') // assertionerror:a,b not exactly equal = = =
4. Assert.deepequal (Actual, expected[, message]),//whether depth matches the opposite: Notdeepequal
var New Buffer (' abc '); var New Buffer (' abc 'buf1 is not the same as Buf2 '); // Assertionerror:buf1 and buf2 are not the same assert.notdeepequal (BUF1, Buf2, ' buf1 and Buf2 '); // like Assertionerror:buf1 and Buf2 .
5.assert.throws (block, [ERROR], [message])//Declare a block to throw an error, ' ERROR ' can be a constructor, a validation function, or a regular expression
Assert.doesnotthrow (block, [message])//Declaration module does not throw an error
//constructor FunctionAssert.throws (function() { Throw NewError ("Wrong value"); }, Error);//RegularAssert.throws (function() { Throw NewError ("Wrong value"); }, /value/);//Custom ErrorAssert.throws (function() { Throw NewError ("Wrong value"); }, function(err) {if(ErrinstanceofError) &&/value/. Test (ERR)) { return true; } }, "Unexpected error");
node. JS Assertion Module assert unit test