Basic Node and Mocha setup

I extensively use Node.js in my daily work and find it more comfortable logging to the terminal console now than creating an html file and do the same in the browser. Writing tests is an often neglected and hated step of the development process but it's an important part of creating good code. I will briefly write down below how to install Node.js and Mocha test framework.

Installing Node.js

First, you will need Node.js installed on your computer. If you don’t have it, go to the website and simply download the installation file of the LTS (stands for long-term support) version (8.9.4 as of writing this) for your operating system.

If you want to, you can also download the Current version (9.4.0 as of today). It contains the latest features. Note that the even numbered versions are supported for a longer time period (see the release schedule) and are recommended for most users.

The installation, which might take a while, is very easy, nothing special, just follow the instructions.

If you already have Node installed, it’s also worth updating to the newest version at times to make sure that you have a secure and well-working platform.

When the installation process finished, you can check if it was successful by opening a terminal and typing node -v. You should see the Node version you just installed.

When installing Node.js, you will also get the npm package manager (version 5.6.0 as of today). You can install third-party modules necessary for the development with the help of npm.

Creating a package.json file

When both Node and npm are installed, we can create a file called package.json. This file contains important information about your project.

First, open a terminal (command line) and create a folder for your project using mkdir PROJECT_NAME. Then navigate to that folder by entering cd PROJECT_NAME.

Once you are in the folder, type npm init. This will start the creation of the package.json file with some questions to answer. It also offers default answers, so if you are happy with them (and you can be in simple cases), just keep pressing Enter to accept them.

If you don’t want to go over these questions, or your project won’t be shared, you can use the npm init --yes shortcut. This will create the package.json file with the default settings and will skip the questions, saving you approximately 16.5 seconds (or more if you read the questions and answer them as well).

The package.json file will be added to the project root folder and you can add options to it as your project grows.

If you want to know more about package.json, the questions and the properties of the object, visit the npmjs site, where a detailed and well explained documentation will help you find answers to your questions.

Setting up the test environment

The importance of writing good test is underestimated by many developers. They hate writing tests saying that “I see it working, why do I have to bother with stupid tests?”. I tended to think that way in the past and honestly, I hated writing unit tests as well. Even many popular tutorials and online courses silently ignore tests.

The truth is that tests will make our code more robust. It happened to me quite a few times that I added a new feature to the code base or modified an existing one and one or more test cases got broken. It always indicated that I wasn’t careful enough with the changes and there was something I missed in the code.

Tests give the developer confidence in their code.

Now let’s see what test tools I use and how to set them up. First, get Mocha. Mocha is a very popular test framework for Node. Navigate to your project folder if you aren’t already there and enter npm install --save-dev mocha. We install Mocha as a developer dependency. This means that we need it for the development phase. After Mocha is installed, you should see it in the devDependencies section of the package.json file (version 5.0.0 as of today). If you are at package.json, you can configure a command for running our tests. Have a look at package.json, and you will see a script object with a test property. It has a value that no test is specified, which is no wonder since we haven’t specified any. Delete this default value and enter the following:

"test": "mocha **/*.test.js"

This command will run Mocha on any folder that contains files that end in .test.js. This is a conventional way of naming test files. All we have to do now is create some test cases and then run npm test, and the Mocha will start running our test cases.

I also use an assertion library called Chai together with Mocha. Type npm install --save-dev chai in the terminal to install it as a devDependency. Depending on personal preference, one can use expect, should or assert for creating assertions. They serve the same purpose in different styles. (An assertion is when we expect something to happen and make a statement about it. If that statement proves to be true, the test will pass, which means that your expectation about that particular feature was correct. If assertion is false, the test will fail and you need to work on the code a little more.)

Conclusion

The above frameworks and libraries are extremely useful tools for web developers. Install them, go through the Getting started sections and start using them. I extensively use these tools in my daily work and will refer to them in the articles as well. Enjoy coding and write tests!