Search code examples
node.jsazure-devopscontinuous-integrationsupertest

Start Node server and run test in CICD


I am currently trying to deploy some tests to Azure pipelines (but this should apply to any CICD framework)

I have an express project, and I am using supertest with mocha to run tests locally and everything is fine.

Now I want to deploy to production, and I want to run the tests in the pipeline before deployment happens.

The thing is that since these tests are running against http, they need a server running.

So, in my pipeline I have

npm install npm start npm test

But the thing is that npm test is not running when the server is started, but instead it just hangs in the server running.

Is there a way to start the tests when the server starts? and then stop the server when the tests are finished?

Or is there a better way to achieve all this?


Solution

  • If you're using supertest I would suggest to export the express app for the tests instead of running it as shown in their docs, and then you would only need to run npm install and npm test.

    For example:

    app.js

    const express = require('express');
    const app = express();
    
    // Add middlewares
    
    module.exports = app;
    

    test.spec.js

    const app = require('../app.js');
    
    request(app)
      .get('/user')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200)
      ...