Node.js – Elegant Ways to Track Functions Calls

507 VIEWS

Node.js is a cross-platform JavaScript runtime environment for executing Javascript outside the browser. You can use Node.js to build an Application Programming Interface for communicating with the server side. Node.js makes it possible to build a full application using one programming language, Javascript, for frontend and backend. The uniqueness of Node.js lies in its ability to run in a single thread process, and can handle multiple concurrent processes. Highly-scalable and real-time apps such as Netflix, Paypal, and Trello were built with Node.js. I am so excited about the new features added in the Assertion module in Node.js. Let’s explore the elegant ways of tracking function calls introduced in Node v14.2.0.

Node v14.2.0 came with three exciting features: assert.CallTracker , assert.verify and assert.report. The purpose of these methods is to enable tracking a function. It is important to note that these methods are experimental; they are not yet recommended for production purposes.

What is Assertion in Node.js?

Assertion is a way of evaluating programming expressions. Node.js has a useful module called assertion that contains various assertion functions. The assert module is mostly used for unit testing in Node.js. Assertions are crucial components in programming because they help us to evaluate the correctness of a piece of code, which is an essential attribute of any high-quality software.

Prerequisites

To understand this tutorial, you will need:

  1. Basic understanding of Node.js.
  2. Install the latest Node version, Node v14.2.0 on your machine.

How to Use the Assertion Module

Our starting point for tracking a function is importing the assertion testing module:

const assert = require('assert')

What is the Purpose of assert.CallTracker?

Once you import the assertion test module, you can now use the assert.CallTracker.The purpose of the assert.CallTracker is to track the number of times a function was called using some of its special methods. The first step in implementing an assert.CallTracker is to create a CallTracker object.

 

Below, you’ll see how to instantiate a CallTracker object:

const assert = require('assert')

const tracker = new assert.CallTracker()

 

If you run your code at this point, you are likely to encounter a TypeError as shown below:

const tracker = new assert.CallTracker();

TypeError: assert.CallTracker is not a constructor

 

This is because in the previous versions of Node, assert.CallTracker had not been introduced as a constructor. To proceed with this tutorial, you will have to install Node v14.2.0. Let’s add a few lines of code to what we have as shown below:

const assert = require('assert')

const tracker = new assert.CallTracker()

function func() {console.log("Hello World")}

const callsfunc = tracker.calls(func, 2)

callsfunc()

process.on('exit', () => {

 tracker.verify()

})

In the code above, we have a function func that prints “Hello World”. The next function we added is callsfunc, which takes the func function and an integer (the number of times callfunc is expected to be called) as parameters. The only way to track the number of times function func was called is to wrap it with tracker.calls() , which keeps record of the number of times the func function is called. 

After that, we can call our wrapped func using the callsfunc so that it is traceable. One last thing to do is to verify if the tracker call (callsfunc) has been called twice, as expected, using tracker.verify()

In the code below, we are interested in tracking the number of times callsfunc function was executed, and match it against the number of executions we set; in this case, two calls. See below for a sample output:

We got an error after printing “Hello Word” once. This is as a result of calling callsfunc() once, instead of twice as defined. Any time the output value of the callsfunc() does not add up to the number of times it’s expected to be called, tracker.report() is implicitly called. 

Tracker report is a JSON array containing detailed information about the error that occurred when tracking a function.

 Below is the structure of a tracker report:

The Structure of the Tracker Report

There are five main components of tracker.report()and they include:

  1. Message: The message field gives information about the number of expected executions against the number of executions that has actually occurred. 
  2. Actual: Reports the number of times callsfunc was executed.
  3. Expected: Displays the number of times callsfunc is expected to be executed. 
  4. Operator: The operator is the function we are tracking wrapped in callsfunc
  5. Stack Trace: Gives us a hint of where the error is emanating from. 

The Expected Number of Executions are Equivalent to the Actual Executions

Let’s take a look at the code below, where the number of times we expected callsfunc() to be executed is equivalent to the number of times it was executed:

const assert = require('assert')

const tracker = new assert.CallTracker()

function func() {console.log("Hello World")}

const callsfunc = tracker.calls(func, 2)

callsfunc()

callsfunc()

console.log(tracker.report())

process.on('exit', () => {

 tracker.verify()

})

Here is the output:

Hello World

Hello World

[]

From the output above, we can see that “Hello world” was printed twice because we called callsfunc() twice. The empty array shown in the output represents the tracker report I explicitly printed. The function was executed the number of times it was expected to be executed. Therefore, no tracker report message is generated.

Key Takeaways:

Let’s recap the main things we discussed in this tutorial: 

  1.  assert.verify : We started by instantiating the Calltracker object. This object has a tracker.calls() method that helps us to track the number of times a function was called. 
  2.  assert.verify : This function helps to confirm the number of times the function was called.
  3.  assert.report : Gives us a detailed report whenever an error occurs due to expected number of function calls not matching the actual function invocation.

Primrose Katena is a software engineer, entrepreneur and technical writer. She strongly believes entrepreneurship and technology are positively impacting the world. She has worked in the IT industry in Zimbabwe and Entrepreneurship in Ghana. Ms. Katena loves sharing tech knowledge with other developers through writing.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

%d bloggers like this: