why TDD is easy with Node app

motivation

I am not a node dev. I am not. But I like to do things using easy tools. I am lazy – in the same way as all of you are lazy

TDD?

Using experience of my colleagues I have come to the conclusion that TDD is nice. No more checking by yourself does the app runs, no more manual smoke test performed. I want to run tests and be sure that positive outcome of them says : app is running OK

runner : Node

I have been using following „language-frameworks” for past ages: Delphi and Java. When using some lanugage you are tied to possibilities of some framework. Every interpreted language need some interpreter. For java it is java for python it is python for Node it is .. node.
I have decided to use it because Node makes writing web-server very easy. First of all get it from web and try running any Node hello

dependency management : npm

Every framework has to have any dependency management tool. I can’t forget how bad it was in Delphi, how many days you had to spend to run app copied to pendrive from your colleague laptop. Npm has is just installing everything you need. You will need npm install to install all dependencies laying in package.json, npm test to run tests and npm start to start your node app.

webserver : express-js

How to make webserver easily? In your node app just write this in your js code

[code lang=”javascript”]
app.get('/', function (req, res) {
res.send('Hello World!');
});
[/code]

Context please check it here. Basically now you have running web-server that serves get at root. You also easily have sent text response.

test

runner – npm

First of all to acomplish test you have to have a runner for it. In npm config package.json you may just define your test runner. I have chosen mocha as my runner and I will not argue that is the best choice. Any runner may be here.

test description – mocha way

Mocha allows for nice test description. Using describe it pair you may do anything you want. One step for success is to make your test descriptive, in the same time they may become your documentation.

assertions with chai

With chai’s expect you may write your test very effective descriptive way which is also readable, isn’t it?

[code lang=”javascript”]
expect(res.status).to.equal(200);
[/code]

calls by superagent

This is easy way how to make web requests. You have nice availibity of textual response or body of the response message (if json is expected). Just shoot and listen 🙂

[code lang=”javascript”]
superagent
.get(SERVER_URL+'/api/last/2')
.end(function (res) {
expect(res.status).to.equal(200);
expect(res.body).to.have.length(2);
done();
});
[/code]