First Time Node - #5 Configuring & Writing Tests

Testing your code is important. People disagree about how important it is, but we can all agree that writing at least some tests is a good practice. In this post, I will show you how to write and run tests on your code using Mocha. For this post, we will continue using the project from my previous post.

Step #1 Install Mocha

We want to install Mocha globally, so open up your terminal and enter: (you may need to use sudo if you are on OSX or Linux)

npm install -g mocha

Now, because we are writing our code using ES2015, we need to add the Babel register. So, from inside your project folder, enter:

npm install babel-register --save-dev

We are going to be using the should library in our tests, so go ahead and install it as well:

npm install should --save-dev

Step #2 Add an npm test Script

Open up your package.json file and add a test script:

{
  ...
  "scripts": {
    ...
    "test": "mocha --compilers js:babel-register"
  }
}

Step #3 Write Some Tests!

Create a folder called test in the root of your project folder. If you are continuing on from my last post, you already have a module called adder. Let's write some tests for that. You can put all your tests in one file or spread them out over multiple files, it's up to you. However you do it, Mocha will find them! So, for this post let's just make one file in your tests folder called tests.js. Inside that file, write:

/* global describe, it */
/* eslint no-unused-vars:0 */

import should from 'should';
import adder from '../src/modules/adder';

describe('adder', function() {
    it('should be a function', function() {
        adder.should.be.a.Function();
    });
    it('should return a number', function() {
        adder(3, 4).should.be.a.Number();
    });
    it('should return the sum of two numbers', function() {
        adder(3, 4).should.equal(7);
    });
});

Let's break this down. At the top of the file we are putting flags for ESLint so it does not display errors all over our tests. We first tell it that describe and it are both global variables, so it should not worry that we did not declare them here (we could also put these in our .eslintrc file, but since these are not global variables across our entire application, it's best that we just put them here). Also, we tell ESLint to disable the no-unused-vars rule, so it does not get upset that we never directly use the variable should.

Next, we imported the should library which we installed earlier. Then we imported our adder module.

describe and it are both global functions provided to us by Mocha. I am not going to go into all the ways you can use Mocha, but if you want to learn more you can visit their website: https://mochajs.org/.

Step #4 Run Your Tests

In your terminal, enter:

npm test

If we did everything right, all your tests should pass. Congratulations!

Conclusion

There you have it! You can now write and run tests on your modules. It really is amazing how easy it often is to do things with npm and Node.

You can view the code from this post on GitHub.

Next time, I will show you how to create a fully-functioning web server in Node using just the express library and plain JavaScript!


This is part five of an ongoing series: