Testing a Node.js application with the native module: assert

Testing a Node.js application with the native module: assert

Testing are a way that you can do to verify if the features of your application are working correctly, either isolated or integrated. This practice became a software development process called TDD (Test-driven development), what consist basically in create a test before build the feature, let it fail, and build the code to pass this test, what do the code to be smaller and focused.

To try this test approach in Node.js, we can use the "assert" module, what provide some basics functions to check your functions and methods. Notice some function from this module are deprecated and others are under test, so I'll just introduce the stable ones.

For this tutorial, I'm using Node.js, version 15.6.0 and npm, version 6.14.11.

To begin, you just import the module "assert" to your code

import assert from "assert"
...

The functions of the "assert" module throw an exception called AssertionError **when some tests failed, but it does nothing when it passes. To verify the result, without fight against a Node.js error, it is possible to put the assertions inside a "try catch".

import assert from "assert";

try {
    assert.strictEqual(5, 10, "The values are not equal"); //test if the values are equal
    console.log("Ok");
} catch(error) {
    if(error instanceof AssertionError) {
        console.error(error);
    }
}

We did our first test that will fail, but what is strictEqual? This function and others will be showed here.

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

Check if the values are equals

  • actual Actual value
  • expected Expected value
  • message Error message

https://nodejs.org/api/assert.html#assert_assert_strictequal_actual_expected_message

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

Inverse of strictEqual. Throw a AssertionError **if the values are equal.

  • actual Actual value
  • expected Expected value
  • message Error message
//...
assert.notStrictEqual(5, 10, "5 is equal to 10 ???");
//...

https://nodejs.org/api/assert.html#assert_assert_notstrictequal_actual_expected_message

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

  • "Tests for deep equality between the actual and expected parameters. "Deep" equality means that the enumerable "own" properties of child objects are recursively evaluated also by the following rules."
  • actual Actual value
  • expected Expected value
  • message Error message
//...
assert.deepStrictEqual(
  {name: "John", age: 20},
  {name: "John", age: 20},
);
//...

https://nodejs.org/api/assert.html#assert_assert_deepstrictequal_actual_expected_message

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

Basically, the inverse of deepStrictEqual. When the assertion is true, a AssertionError **is throw.

  • actual Actual value
  • expected Expected value
  • message Error message
//...
assert.notDeepStrictEqual(
  {name: "John", age: 20},
  {name: "Mary", age: 20},
  "The objects are equals"
);
//...

https://nodejs.org/api/assert.html#assert_assert_notdeepstrictequal_actual_expected_message

assert.assert(value[, message])

Tests if the value of the expression is true. If it does not, throw an AssertionError.

  • value Expression to evaluate
  • message Error message
//...
assert.assert(10 < 5, "5 is not greater than to 10");
//...

assert.ifError(value)

Checks if the value is either null orundefined. If it does not, throw thevalue inside the AssertionError

  • value
//...
assert.ifError("it is not undefined");
//...

You can also test promises with:

assert.rejects(asyncFn[, error][, message])

Check if the asyncFn return a promise rejected. If it does not, a AssertionError **are thrown.

  • "If asyncFn is a function, and it throws an error synchronously, assert.rejects() will return a rejected Promise with that error. If the function does not return a promise, assert.rejects() will return a rejected Promise with an ERR_INVALID_RETURN_VALUE error. In both cases the error handler is skipped."
  • asyncFn Function that return a promise/ promise
  • error Result from the promise rejected
  • message Error message
//...
assert.rejects(async () => {
  return await Promise.reject({
    name: "John",
    age: "20"
  });
}, {
  name: "John",
  age: "20"
}, "Promise resolved!");

// or...
assert.rejects(Promise.reject({
    name: "John",
    age: "20"
  }), {
  name: "John",
  age: "20"
}, "Promise resolved!");
//...

https://nodejs.org/api/assert.html#assert_assert_rejects_asyncfn_error_message

And, if you need, just use the assert.fail([ḿessage]) to throw a AssertionError with this message.

The Node.js assert API is simple and easy to use, but can be useful if a test suit like Jest is too much to your requirements at a certain moment. Knowing the existence of this module and how you can use it can be helpful also if you want to take a Node.js certification, according to which I read.

At this article, I tried to bring here the functions of this module that is not deprecated, recommended by the documentation and what I guess which would be most used in the day life.

Thank you to read and feel to comment or correct me about something in the article. I hope this help you in some way.

Useful links:

 
Share this
Proudly part of