Software testing is integral to software engineering. Tests are created as key checkpoints to verify whether objectives are being met or not.

For instance, you are building a web chat application and you need to add a new feature to the app. Testing gives you confidence to be able to refactor and add new features with the assurance that the app’s old verifiable functions are tested.

By the end of this blog, you will get in-depth insights on how to write assertions using the Node.js assert module and mocha.

So go grab a cup of coffee.

jim_carrey_typing.gif

What are Assertions?

Assertions are test conditions that must be verified to determine whether certain software development objectives are being met. The inclusion of “conditions” in this definition indicates that it encapsulates some testable logic that should be verified as specified about a target under the test.

So to verify a particular behaviour, we can assert a series of logical conditions that satisfy the design of that behaviour.

For instance, to verify the behaviour of a carting system, we may simply assert:

To verify that the function conforms to the requirement of adding two numbers.

Common Asserts with NodeJS assert module

The assert module provides a set of assertion functions to verify testing expressions. If the functions provided by this module evaluate to 0 or false, the function raises an exception, AssertionError, which might cause the program to terminate if not handled.