A fresh look at testing Node.js projects: Mocha, Should and Travis

A fresh look at testing Node.js projects: Mocha, Should and Travis

Today, I finally decided to spend some time around Travis. It’s been a weeks since that little green image on top of GitHub homepages has been buzzing me. Well, to be totally honest, this isn’t how I started my evening. First, after 2 years of good and faithfull service, I decided to drop Expresso and give a chance to Mocha. Because Expresso enriches the assert module with one or two functions which I became addicted to, I also had to find the same functionalities into another assertion library which lead me to testing Should. It is very pleasant to see those two working together, as in the Unix tradition: small, powerful and naturally integrated.

Written by the same author than Expresso, Mocha is meant to replace it. It’s been the case since quite a while but I’ve always been reluctant to jumping into BDD testing. BDD stands for Behavior Driven Development and is an alternative way to express your tests. For example, test names are expressed as a full sentence. Its goal is to encourage collaboration between developers, QA and non-technical or business participants in a software project. This is probably applicable in some workspaces but from a personal experience, it can’t expect our clients to participate in test coverage. Cucumber is probably the most popular BDD tool, it has its fans but I’ve been too lazy in the past to jump into BDD. Facing any sample, It feels like learning a new language to write tests. Probably it was a wrong impression and it might be much easier than I think.

Now let’s have a look at Mocha. the API is quite simple, mainly two functions which are describe and it. They both take a text as a first argument which combining together produce a sweet sentence during the run. An example:

Notice how Mocha discover and let yet decide whether you wish to run your tests synchronously or not, even mixing the two modes. Behind the hood, it’s making use of the not so popular length property attached to a function. There are few additionnal methods that come handy to modify settings and to setup and cleanup your tests. For illustration purpose, here is an example inspired from the mecano test suite:

The functions beforeEach and afterEach allow code execution before and after each test. The function timeout modify the maximum execution time of a tests (by default 2 seconds).

Here, let’s introduce the usage of Should. Its purpose like any assertion library is to detect incorrect returned variable. At first, the BDD style API may look complicated but I found it natural and I didn’t have to fight against it. Well, I said it all. This is essentially what I’m expecting from such a library, not opening my browser to figure out how to use the API. After 10mn of usage, Should has proved to be easy, delivered its promises and enhance code readability (to my opinion).

Finally, I decided to deal with this Travis status. Getting the image popping up into the mecano GitHub page was easy. Travis setup and GitHub integration for a newbie took less that 15 minutes. To make it green took a little longer. Mecano is making a few assumptions when running its test. It must be online, have GIT installed and be able to SSH itself. It turns out I was positively surprise on the first run, only 5 tests when not passing. Most of them were my fault and I fixed them. Another one was Should usage of JavaScript which was incompatible with the one used by Node.js in version “0.4”. That left me with only one issue, setting up the Travis user account in such a way it can SSH itself without a password asked. In the end, my “.travis.yml” configuration file looks like:

During this presentation, I did not provide any instruction related to installation and configuration because each project is very well documented. Thanks to the author of those tools for their appreciable work.

By | 2017-11-21T20:22:53+00:00 February 19th, 2012|Categories: Node.js|0 Comments

About the Author:

Leave A Comment

Time limit is exhausted. Please reload the CAPTCHA.