Amit Gupta Amit Gupta - 4 years ago 104
Node.js Question

Automate Functional Testing in node.js

I checked some NPM libraries to test a webpages or web-services. But all of them expect that the server is already running. Since I want to automate functional testing, how can I setup NPM package in such a way that


  1. It can start the server

  2. Test the application

  3. Stop the server



So that I can test it locally, as well as on online CI tools like travis-ci or circleci.

Case 1: Webservice

I wrote a NPM package which starts nodejs HTTP(s) server. It can be started from command line
$stubmatic
. Currently, I use 2 approaches to test it,


  1. manual : I manually start it from command line. Then run the tests.

  2. Automatic: I use
    exec
    module to run a unix command which can start the application and run
    pkill
    command to kill the application. But for this automation, my application need to be installed on testing machine.



Case 2: Website

I have create a NPM package:
fast-xml-parser
and created a demo page within the repo so that I can be tested in the browser. To test the demo page, I currently start a local server using
http-server
npm package manually. Test the application.

What can be the better way to write automate functional tests for node js applications?

Note:


  • I never used task runners like gulp or grunt. So I'm not sure if they can help in this case.

  • In case 1, my application starts node js native HTTP server. I'm not using any 3rd party application like
    express
    currently.


Answer Source

Case 1: Webservice

Problem 1 As nodejs server.close() was not working. I copied paste this snippet in every test file which is starting my webservice.

try{
    server.setup(options);
    server.start();
}catch(err){
    console.log(err);
}

Once all the tests are completed, server stops.

**Problem 2 I was using chai-http incorrectly. Here is the complete working solution.

//Need to be placed before importing chai and chai-http
if (!global.Promise) {
  global.Promise = require('q');
}

var server = require('.././lib/server');
var chai = require('chai')
  , chaiHttp = require('chai-http');

 chai.use(chaiHttp);

try{
    server.setup(someoptions);
    server.start();
}catch(err){
    console.log(err);
}

describe('FT', function () {

  describe('scenario::', function () {

    it('responds to POST', function (done) {
        chai.request("http://localhost:9999")
            .post('/someurl')
            .then(res => {
                expect(res.status).toBe(200);
                //console.log(res.text);
                done();
            }).catch( err => {
                console.log(err);
                done();
            });
    });

});

Case 2: Website This was quite simple.

  • I used http-server to start the server so my html files can be accessed.
  • I used zombie js for browser testing. (There are many other options available for browser testing)

Here is the code

process.env.NODE_ENV = 'test';

const Browser = require('zombie');
const httpServer = require('http-server');

describe("DemoApp", function() {
  var browser = new Browser({site: 'http://localhost:8080'});
  var server = httpServer.createServer();
  server.listen(8080);

  beforeEach(function(done){
      browser.visit('/', done);
  });

  describe("Parse XML", function() {

    it("should parse xml to json", function(done) {
       browser.pressButton('#submit');
       browser.assert.text('#result', 'some result text');
       done();
    });
   });

   afterEach(function(){
       server.close();
   })
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download