Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Node .js assertion test


May 10, 2021 Node.js


Table of contents


Assertion test

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')

assert.fail(actual, expected, message, operator)

Use the parameters operator to test whether the parameters actual expected and expected (expected) are equal.

assert(value[, message]), assert.ok(value[, message])

Test whether value is true this function and assert.equal(true, !!value, message); Equivalent.

assert.equal(actual, expected[, message])

Determines whether the actual real and whether the expected expected value is equal.

assert.notEqual(actual, expected[, message])

Determines whether the actual and the expected are not equal.

assert.deepEqual(actual, expected[, message])

Perform a depth comparison to determine whether the actual and expected are equal.

assert.notDeepEqual(actual, expected[, message])

The depth compares whether the two parameters are not equal.

assert.strictEqual(actual, expected[, message])

The depth compares whether the two parameters are equal.

assert.notStrictEqual(actual, expected[, message])

This function uses the operator '!'' to strictly compare whether the two parameters are not equal.

assert.throws(block[, error][, message])

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"
    );

assert.doesNotThrow(block[, message])

Declaring block does not throw an error, see assert.throws

assert.ifError(value)

Determines whether the parameter value is false and, if true, throws an exception. Typically used to test the first parameter in the callback, error.