Skip to content

Commit

Permalink
Split out server and app
Browse files Browse the repository at this point in the history
We experienced a lot of issues replacing mocha because server.js
had a lot of complicated code and it wasn’t clear what was happening.
This commit starts to address this issue by splitting the server and
the app logic into separate files (server.js and app.js)

As mentioned in this blog
http://www.albertgao.xyz/2017/05/24/how-to-test-expressjs-with-jest-and-supertest/.
There is no need to add listeners to the app for testing and seems to
be a best practice.
  • Loading branch information
hannalaakso authored and aliuk2012 committed Nov 16, 2018
1 parent c82d3c6 commit 1e6d8c3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion __tests__/spec/sanity-checks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env jest */
var request = require('supertest')
var app = require('../../server.js')
var app = require('../../app.js')
var path = require('path')
var fs = require('fs')
var assert = require('assert')
Expand Down
24 changes: 0 additions & 24 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const path = require('path')

// NPM dependencies
const bodyParser = require('body-parser')
const browserSync = require('browser-sync')
const dotenv = require('dotenv')
const express = require('express')
const nunjucks = require('nunjucks')
Expand Down Expand Up @@ -58,13 +57,11 @@ var useAuth = process.env.USE_AUTH || config.useAuth
var useAutoStoreData = process.env.USE_AUTO_STORE_DATA || config.useAutoStoreData
var useCookieSessionStore = process.env.USE_COOKIE_SESSION_STORE || config.useCookieSessionStore
var useHttps = process.env.USE_HTTPS || config.useHttps
var useBrowserSync = config.useBrowserSync
var gtmId = process.env.GOOGLE_TAG_MANAGER_TRACKING_ID

env = env.toLowerCase()
useAuth = useAuth.toLowerCase()
useHttps = useHttps.toLowerCase()
useBrowserSync = useBrowserSync.toLowerCase()

var useDocumentation = (config.useDocumentation === 'true')

Expand Down Expand Up @@ -349,25 +346,4 @@ app.use(function (err, req, res, next) {
console.log('\nGOV.UK Prototype Kit v' + releaseVersion)
console.log('\nNOTICE: the kit is for building prototypes, do not use it for production services.')

// Find a free port and start the server
utils.findAvailablePort(app, function (port) {
console.log('Listening on port ' + port + ' url: http://localhost:' + port)
if (env === 'production' || useBrowserSync === 'false') {
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
34 changes: 34 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// NPM dependencies
const browserSync = require('browser-sync')

// Local dependencies
const app = require('./app.js')
const config = require('./app/config.js')
const utils = require('./lib/utils.js')

// Set up configuration variables
var useBrowserSync = config.useBrowserSync.toLowerCase()
var env = (process.env.NODE_ENV || 'development').toLowerCase()

// Find a free port and start the server
utils.findAvailablePort(app, function (port) {
console.log('Listening on port ' + port + ' url: http://localhost:' + port)
if (env === 'production' || useBrowserSync === 'false') {
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

0 comments on commit 1e6d8c3

Please sign in to comment.