Loading...

Jammie Mountz

Software Engineer

Basic testing your Node.js server with Mocha

It can be difficult to get started with testing if you’re not familiar with the basic structure testing frameworks need to run. I was put in charge of testing for a project and found that although I had experience writing tests, setting up the testing environment was a whole other beast. This post is for Mocha tests on a Node.js server running Express.

First, Mocha needs to be installed. I just went ahead and installed it globally and then as a part of my node modules. You can do this from the command line in the root directory of your project.


$ npm install -g mocha
$ npm install --save-dev mocha

Next, the testing file structure needs to be created. I followed naming conventions already established. I simplified much of my structure, so this is general, but your project should look something like this. The files I created are bolded.


- myProject
- server
- client
- node_modules
- test
- test.js

- package.json

Our Mocha tests are going to be written in test.js. We start the file with our requires. You’ll need to require mocha, your server, and the assert library.

[code lang=”js”]
var server = require(‘../server/server’);
var http = require(‘http’)
var assert = require(‘assert’)
[/code]

This brings in your dependencies that you can then use to write your test. Mocha allows you to use a ‘before each’ hook to set up things you want to do before every test. If we’re testing our server, naturally we’d want to start an instance of that server before each of our tests. Make sure your server isn’t already running before running the test or you’ll get the “already in use” error.

[code lang=”js”]
describe(‘server’, function () {
before(function () {
server.listen(3000);
});

after(function () {
server.close();
});
});
[/code]

The server variable points to our server.js file we required at the head of the page, and we start listening on the port 3000 (which may be different for your tests). We close the server after all the tests have run.

Now we can write the tests we want performed on the server. I wrote a test that makes a “GET” request to the home page, expecting a 200 code, and a test that makes a “GET” to our API route, and then processes the data retrieved to see if it’s an object.

[code lang=”js”]
// this test makes a get request to the homepage
describe(‘a request to the homepage should return a 200 code’, function () {
it(‘should return 200’, function (done) {
http.get(‘http://localhost:3000’, function (res) {
assert.equal(200, res.statusCode);
done();
});
});
});

// this test makes a get request to outlets api
describe(‘a request to the outlets api should return data for outlets’, function () {
it(‘should return 200’, function (done) {
http.get(‘http://localhost:3000/api/outlets’, function (res) {var data = ”;

res.on(‘data’, function (chunk) {
data += chunk;
});

res.on(‘end’, function () {
var outlets = JSON.parse(data);
assert.equal(‘object’, typeof outlets);
done();
});
});
});
});
[/code]

You can use the res.on(‘end’) function to manipulate the data received from the API and write tests to guarantee the data is what should be returned. Passing in ‘done’ as an argument to the function and then calling it after the data is received allows us to run tests asynchronously.

That’s Mocha testing on Node.js!

Fluent in Spanish and JavaScript, salsa dancer, lover of material design.

Living in San Francisco, CA