Build a Unit Test library with Node.js from Scratch

Fahad Haidari
3 min readJan 3, 2020

In this short article I’m going to show you the basics of how to write your own Unit Test library from scratch, using Node.js. Note that you can also use JavaScript (in the browser) to achieve the same results.

Note: we’ll not going to cover things related to how to run a custom test command to run our tests; we’ll only have one file (index.js) which we’ll run to execute the test cases which we are going to write using our testing library.

Let’s begin

Let’s create a directory with any name then create two files within that directory:

  1. test-lib.js, you can name it whatever you want, I’m not going to judge you. This file is going to contain the actual testing library.
  2. index.js, we’ll use later on, to write our tests using test-lib.js.

Now, in our test-lib.js file we’ll start writing our logic…

First, we want to create the describe function; you can think about it as the description of the actual set of related tests. i.e. “testing the Math functions”

const describe = function(msg, callback) {
console.log(`* ${msg}`);
callback();
};

It’s a straight forward function that takes accepts two params; the message (the actual description) and the a callback…

--

--