From e86546bdf058d072a6fa73607aef2a99b5deaba7 Mon Sep 17 00:00:00 2001
From: Joe Lanman <joelanman@gmail.com>
Date: Thu, 20 Oct 2016 18:47:02 +0100
Subject: [PATCH] separate server from run-server for mocha

---
 gulp/nodemon.js            |  2 +-
 run-server.js              | 33 +++++++++++++++++++++++++++++++++
 server.js                  | 26 --------------------------
 test/spec/sanity-checks.js | 12 +++++++++---
 4 files changed, 43 insertions(+), 30 deletions(-)
 create mode 100644 run-server.js

diff --git a/gulp/nodemon.js b/gulp/nodemon.js
index 23f6d744ba..13192b7a64 100644
--- a/gulp/nodemon.js
+++ b/gulp/nodemon.js
@@ -12,7 +12,7 @@ var config = require('./config.json')
 
 gulp.task('server', function () {
   nodemon({
-    script: 'server.js',
+    script: 'run-server.js',
     ext: 'js, json',
     ignore: [config.paths.public + '*',
              config.paths.assets + '*',
diff --git a/run-server.js b/run-server.js
new file mode 100644
index 0000000000..d6523ce720
--- /dev/null
+++ b/run-server.js
@@ -0,0 +1,33 @@
+var utils = require('./lib/utils.js')
+var app = require('./server.js')
+var browserSync = require('browser-sync')
+var packageJson = require('./package.json')
+
+// Grab environment variables specified in Procfile or as Heroku config vars
+var releaseVersion = packageJson.version
+var env = process.env.NODE_ENV || 'development'
+
+console.log('\nGOV.UK Prototype kit v' + releaseVersion)
+// Display warning not to use kit for production services.
+console.log('\nNOTICE: the kit is for building prototypes, do not use it for production services.')
+
+// start the app
+utils.findAvailablePort(app, function (port) {
+  console.log('Listening on port ' + port + '   url: http://localhost:' + port)
+  if (env === 'production') {
+    app.listen(port)
+  } else {
+    app.listen(port - 50, function () {
+      browserSync({
+        proxy: 'localhost:' + (port - 50),
+        port: port,
+        ui: false,
+        files: ['public/**/*.*', 'app/views/**/*.*'],
+        ghostmode: false,
+        open: false,
+        notify: false,
+        logLevel: 'error'
+      })
+    })
+  }
+})
diff --git a/server.js b/server.js
index 010334a5b9..f365c62df1 100644
--- a/server.js
+++ b/server.js
@@ -8,7 +8,6 @@ var favicon = require('serve-favicon')
 var app = express()
 var documentationApp = express()
 var bodyParser = require('body-parser')
-var browserSync = require('browser-sync')
 var config = require('./app/config.js')
 var utils = require('./lib/utils.js')
 var packageJson = require('./package.json')
@@ -182,29 +181,4 @@ if (useDocumentation) {
   })
 }
 
-console.log('\nGOV.UK Prototype kit v' + releaseVersion)
-// Display warning not to use kit for production services.
-console.log('\nNOTICE: the kit is for building prototypes, do not use it for production services.')
-
-// start the app
-utils.findAvailablePort(app, function (port) {
-  console.log('Listening on port ' + port + '   url: http://localhost:' + port)
-  if (env === 'production') {
-    app.listen(port)
-  } else {
-    app.listen(port - 50, function () {
-      browserSync({
-        proxy: 'localhost:' + (port - 50),
-        port: port,
-        ui: false,
-        files: ['public/**/*.*', 'app/views/**/*.*'],
-        ghostmode: false,
-        open: false,
-        notify: false,
-        logLevel: 'error'
-      })
-    })
-  }
-})
-
 module.exports = app
diff --git a/test/spec/sanity-checks.js b/test/spec/sanity-checks.js
index d377806355..45e9d52ec7 100644
--- a/test/spec/sanity-checks.js
+++ b/test/spec/sanity-checks.js
@@ -1,4 +1,5 @@
-/* global describe, it, before */
+/* eslint-env mocha */
+
 var request = require('supertest')
 var assert = require('assert')
 var path = require('path')
@@ -9,10 +10,15 @@ var fs = require('fs')
  */
 describe('The prototype kit', function () {
   var app
+  var server
 
-  before(function (done) {
+  before(function () {
     app = require('../../server')
-    done()
+    server = app.listen(3000)
+  })
+
+  after(function () {
+    server.close()
   })
 
   it('should generate assets into the /public folder', function () {