vendredi 24 juin 2016

How to test server in Node.JS using testing tools?

I am developing a simple login module which performs some functions. In my case it performs Registration, Login, Update and Delete.

Here is my test.js file:

describe('Testing Controller', function() {
   describe('Testing server', function () {
       var server;
       beforeEach(function () {
           server = require('../index');
       });
       afterEach(function () {
           server.close();
       });

       it('Responds to /', function testSlash(done) {
           request(server).get('/page').expect(200,done);
       });

       it('404', function testPath(done) {
           request(server).get('/test404').expect(404,done);
       });
   });
});

Here is the server code in my index.js file:

var env = process.env.NODE_NEW || '/page';
if ('/page' == env) {
  app.set('view engine', 'jade');
  app.set('views', __dirname + '/views');
  app.use(bodyParser.urlencoded({extended: true}));
  app.use(express.static(path.join(__dirname,'public')));
}

I also used this code additionally in my index.js file:

var server = app.listen(3000, function () {
  var port = server.address().port;
  console.log('Server is Running on port: %s',port);
});

module.exports = server;

I want to write tests for testing my server for the following cases in my Login module:

  • Testing if server is running
  • Testing the port
  • Testing all the 4 pages (localhost:3000/login, same for registration, update and delete)

I am currently using Assert and Supertest. I can upload the rest of my code if needed.

Aucun commentaire:

Enregistrer un commentaire