node. JS Assertion Module assert unit test

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.