nodejs
  1. nodejs-assertion

Node.js Assertion

Node.js provides a built-in assert module that allows you to perform assertions in your code. Assertions are useful for testing and debugging code, allowing you to ensure that your program is running as intended. In this tutorial, we'll discuss how to use the assert module in Node.js.

Syntax

The syntax for using the assert module in Node.js is as follows:

const assert = require('assert');

assert(condition);
assert(condition, message);
assert.deepEqual(actual, expected);
assert.deepStrictEqual(actual, expected);
assert.doesNotReject(asyncFunction);
assert.doesNotThrow(function, error, message);
assert.equal(actual, expected);
assert.fail(message);
assert.ifError(value);
assert.notDeepEqual(actual, expected);
assert.notDeepStrictEqual(actual, expected);
assert.notEqual(actual, expected);
assert.notStrictEqual(actual, expected);
assert.ok(value);
assert.rejects(asyncFunction);
assert.strictEqual(actual, expected);
assert.throws(function, error, message);

These are the assertion methods provided by the assert module.

Example

Let's say we have a function that takes two numbers and returns their sum. We can use the assert module to test this function as follows:

const assert = require('assert');

function add(a, b) {
   return a + b;
}

assert.equal(add(2, 3), 5); // passes
assert.equal(add(2, 3), 6); // fails

In the example above, we've used the assert module to test the add function. We've checked that the sum of 2 and 3 is equal to 5, which passes. We've also checked that the sum of 2 and 3 is equal to 6, which fails.

Output

When we run the example code above, the output will be:

AssertionError: 5 == 6
    at Object.<anonymous> (test.js:7:8)

This is because the assertion failed – we expected the sum of 2 and 3 to be 6, but it was actually 5.

Explanation

In the example above, we used the assert module to test the add function. We used the assert.equal method to check that the sum of 2 and 3 is equal to 5, which passed. We then used the same method to check that the sum of 2 and 3 is equal to 6, which failed. The assert module threw an AssertionError with a message that indicates the comparison that failed.

Use

The assert module is particularly useful for testing code. You can use it to assert that certain conditions are true, and if they're not, your program will throw an AssertionError. You can use it to test functions, APIs, and more.

Important Points

  • The assert module comes built-in with Node.js and doesn't require any external packages to use.
  • The assertion methods compare two values for equality, and if the comparison fails, an AssertionError is thrown.
  • You can use the assert module to test both synchronous and asynchronous functions.

Summary

In this tutorial, we discussed how to use the assert module in Node.js to perform assertions in your code. We covered the syntax, example, output, explanation, use, and important points of the assert module in Node.js. With this knowledge, you can now use the assert module to test and debug your Node.js code.

Published on: