Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I start my nodejs application automatically for tests

I have a nodejs restful style service which has no front end, it just accepts data and then does something with it.

I have unit tested most of the method level stuff I want to, however now I want to basically do some automated tests to prove it all works together. When I am using ASP.MVC and IIS its easy as the server is always on, so I just setup the scenario (insert dummy guff into DB) then make a HttpRequest and send it to the server and assert that I get back what I expect.

However there are a few challenges in nodejs as the applications need to be run via command line or some other mechanism, so given that I have an app.js which will start listening, is there some way for me to automatically start that going before I run my tests and then close it once my tests are finished?

I am currently using Yadda with Mocha for my testing so I can keep it written in a BDD style way, however I am hoping the starting of the web app is agnostic of the frameworks I am using.

like image 620
Grofit Avatar asked Nov 02 '13 09:11

Grofit


People also ask

How do I run a node js program automatically?

To restart the application automatically, use the nodemon module. This local installation of nodemon can be run by calling it from within npm script such as npm start or using npx nodemon.

How do I keep node running app?

js application locally after closing the terminal or Application, to run the nodeJS application permanently. We use NPM modules such as forever or PM2 to ensure that a given script runs continuously. NPM is a Default Package manager for Node.

Can you write tests in node without library?

If you've ever written tests for a Node. js application, chances are you used an external library. However, you don't need a library to run unit tests in Javascript. This post is going to illustrate how to make your own simple testing framework using nothing but the standard library.


3 Answers

Just expose some methods to start and stop your webserver. Your app.js file could be something like this:

var app = express()
var server = null
var port = 3000

// configure your app here...

exports.start = function(cb) {
  server = http.createServer(app).listen(port, function () {
    console.log('Express server listening on port ' + port)

    cb && cb()
  })
}

exports.close = function(cb) {
  if (server) server.close(cb)
}

// when app.js is launched directly
if (module.id === require.main.id) {
  exports.start()
}

And then in your tests you can do something like this (mocha based example):

var app = require('../app')

before(function(done) {
  app.start(done)
})

after(function(done) {
  app.close(done)
})
like image 143
gimenete Avatar answered Dec 30 '22 09:12

gimenete


Have a look to supertest https://github.com/visionmedia/supertest You can write test like

describe('GET /users', function(){
 it('respond with json', function(done){
    request(app)
      .get('/user')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200, done);   
  }) 
 })
like image 34
alfonsodev Avatar answered Dec 30 '22 07:12

alfonsodev


Using gimenete's answer, here's an example of a service (server) with async await and express:

service.js:

const app = require('express')()
const config = require('./config')

let runningService

async function start() {
  return new Promise((resolve, reject) => {
    runningService = app.listen(config.get('port'), config.get('hostname'), () => {
      console.log(`API Gateway service running at http://${config.get('hostname')}:${config.get('port')}/`)
      resolve()
    })
  })
}

async function close() {
  return new Promise((resolve, reject) => {
    if (runningService) {
      runningService.close(() => {
      })
      resolve()
    }
    reject()
  })
}

module.exports = {
  start,
  close
}

service.spec.js:

const service = require('../service')

beforeEach(async () => {
  await service.start()
})

afterEach(async () => {
  await service.close()
})
like image 21
Kunal Avatar answered Dec 30 '22 08:12

Kunal