Not everyone likes the JavaScript language equally. One of the reasons for this might be the fact that JavaScript doesn’t provide you with much support while writing your code. But for me, this actually isn’t a very big issue. On the contrary, I think this is a good thing in the long run. Let me explain:

When you minimize the built-in support, every mistake you make will teach you a hard lesson. In doing that, it creates responsible developers who worry about their code running smoothly, regardless of the environment or machine. This means they will be more meticulous and thoughtful with how they write their code. And once you know that each mistake is costly, you tend to avoid making those mistakes. Inevitably, this will lead you down the path of writing more Unit Tests and other best practices such as BDD and TDD. Luckily for the Node.js programmers, there are some awesome tools available to help you write unit tests easily. We’ll be looking into some of them here.

The basics

Unit Tests consist of three parts:

  1. Arrange: First you arrange all the preconditions and input values to be able to perform the test.
  2. Act: Here, you use the preconditions and inputs to act on the unit (function, method, object) which is under test.
  3. Assert: Lastly, you get the output of the previous step and make sure that this output is as you expected.

Tools

One of the most convenient tools is the built-in assertion library in Node.js. All you have to do to start using it is the following line:

var assert = require('assert');

This built-in assertion library is extremely powerful. You can take a longer look at it here. But if you want to take it a step further, you will soon stumble upon some of my favorite tools like Mocha and Chai.

Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting while mapping uncaught exceptions to the correct test cases.  mochajs.org

Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any JavaScript testing framework. chaijs.com

Get started

Open up a terminal and run:

npm init

Once you have gone through all the prompts you can install the modules mentioned above:

npm install --save-dev mocha chai

For this example, we shall use a very simple program which multiplies 2 inputs and outputs the result.

So our code looks like this:

function multiply(x,y)
{
     return x*y;
}
module.exports=multiply;

The very last line in that function exports the module so it can be required in our test.

var expect = require('chai').expect;
var multiply= require('../multiply');

describe('multiply()', function () {
  it('should multiply two numbers', function () {

    // 1. ARRANGE
    var x = 5;
    var y = 3;
    var result1 = x * y;

    // 2. ACT
    var result2 = multiply(x, y);

    // 3. ASSERT
    expect(result2).to.be.equal(result1);

  });
});

Next step is to make sure you have your code and the test file structured as follows:

test
  - test.js
- multiply.js

And now you can run your test. Make sure that your package.json has the “test”:“mocha” in the scripts section such as follows:

{
  "name": "multiplication",
  "version": "1.0.0",
  "description": "",
  "main": "test.js",
  "directories": {
    "test": "test"
  },
  "scripts": {
    "test": "mocha"
  },
  "author": "",
  "license": "ISC"
}

Now if you return to your terminal and run the command “npm test” you will see that your test passes.

Conclusion

A setup as described in this post is fairly easy to construct and once you have the frameworks up and running, writing tests is much easier. This will help you to get more reliable code out there at a much faster rate. It will also enable you to start implementing other practices such as TDD which will increase your development speed even more.

Let me know what you think about the approach mentioned above and what you would like to know or improve.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here

*