0

What is the best practice way when invoking tests with npm to start a Node.js server and then run the test command? I am trying the following in my package.json, but it is failing. Note, I need to invoke node api.js in the background and then I wish to exit it after the jasmine-node command finishes.

"scripts": {
    "test": "node api.js & && jasmine-node test"
}
1
  • Have you considered booting the app within your tests? Commented Aug 9, 2016 at 13:38

2 Answers 2

1

You can give a try with Supertest, thus you simulate your server (setting up an agent) and shut it down when tests are done. This also avoids some tricky hacks to shut down the server process once tests are done.

Edit: The advantage with supertest rather than frisby is that you don't have to call a full URI, you can setup an agent from your app object (example is from supertest readme):

var request = require('supertest');
var express = require('express');

var app = express();

app.get('/user', function(req, res) {
  res.status(200).json({ name: 'tobi' });
});

request(app)
  .get('/user')
  .expect('Content-Type', /json/)
  .expect('Content-Length', '15')
  .expect(200)
  .end(function(err, res) {
    if (err) throw err;
  });
Sign up to request clarification or add additional context in comments.

2 Comments

I am already using frisbyjs.com (frisby) anyway to do it with that?
It seems that you have to call an explicit URI with Frisby. With supertest you can setup your own agent (see answer edit).
0

Ended up writing a simple bash script bin/test-runner.bash to handle this.

#!/usr/bin/env bash
set -eo pipefail; [[ $TRACE ]] && set -x

node api.js & SERVER_PID=$!
sleep 1
jasmine-node test
kill "$SERVER_PID"

Then modified the package.json test command to ./bin/test-runner.bash. The only ugly thing is the arbitrary sleep command, but it works consistently for now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.