0

I am using Koa for web development in NodeJS, I have a server file, which does nothing but to start the server and initialise few middlewares. Following is the sample code

server.js

const Koa = require('koa');
var Router = require('koa-router');
var bodyParser = require('koa-bodyparser');
var app = new Koa();
var router = new Router();
app.use(bodyParser());
router.post('/abc', AbcController.abcAction);
router.post('/pqr', PqrController.pqrAction);

app.use(router.routes());
app.listen(3000);

When we run npm start the server will start on 3000 port and now I want to write unit test case for this file using mocha, chai and sinon.

One way is to create a test file lets say server_test.js and do something like the following(just an example):

var server = require(./server);
server.close();

For this we need to add the following lines to server.js

var server = app.listen(3000);
module.exports = server;

Is this a good practice to do? I think we should not expose server in this fashion?

As we don't have self created function here in this file, is testing really required?

Should we also exclude such files from sonarqube coverage?

Any other better suggestion is always welcome. Need your help guys. Thank you.

Muthurathinam
  • 962
  • 2
  • 11
  • 19
chaitanya90
  • 697
  • 2
  • 8
  • 24

2 Answers2

0

You can use chai-http for testing the endpoint

this is what I use for my project

const chai = require('chai');
const chaiHttp = require('chai-http');
const expect = chai.expect;

const app = require('../app');

describe('/GET roles', function () {
  it('should return bla bla bla',
    function (done) {
      chai.request(app)
        .get('/roles')
        .end(function (err, res) {
          expect(res.status).eql(200)
          expect(res.body).to.have.property('message').eql('Get role list success');
          expect(res.body).to.have.property('roles');
          expect(err).to.be.null;
          done();
        });
    }
  );
});
Luthfi
  • 274
  • 4
  • 12
0

There are primarily 2 ways through which you can actually handle rest cases.

One is to put your test cases along with your source code file. ( in your case it would be server.spec.js ). I personally prefer this way as it encourages code modularity and make your modules totally independent.

Another way is to create another directory, let say test, where you can put your entire test cases according to same directory structure as followed by the main application. This is really useful for applications where test cases are only considered while they are in development phase and then at time of production you can simply ignore sending these files.

Also, I usually prefer following the concepts of functional programming as it really helps to test each block of code independently.

Hope this helps

  • if I need to test my server file in different test file then I will have to import `server`... is that a good practice? – chaitanya90 May 14 '18 at 09:34
  • According to me. No. Create a function let say ‘init’ and write a test case for that function using stubs and spies. Look into the module ‘rewire’ and you won’t have to export any private fiction just for the sake of testing that function. – Parth Mahajan May 14 '18 at 09:37
  • Also, your test cases should be independent. If you think you still require some important file for test cases to work then pass it like this: mocha —require your_necessary_file.js – Parth Mahajan May 14 '18 at 09:48