From d39327342ccc0bd55b87cc0b11625e08ca0eb0f7 Mon Sep 17 00:00:00 2001 From: Andy Mantell Date: Mon, 17 Oct 2016 13:41:18 +0100 Subject: [PATCH 1/4] Setting up a very simple test suite to check the server and build tasks run across nodejs 4, 5 & 6 --- .travis.yml | 3 +++ gulp/tasks.js | 12 ++++++++++-- package.json | 6 +++++- server.js | 2 ++ test/mocha.opts | 1 + test/spec/sanity-checks.js | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 test/mocha.opts create mode 100644 test/spec/sanity-checks.js diff --git a/.travis.yml b/.travis.yml index 80efb7c9ae..937532a592 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,9 @@ language: node_js node_js: - 4.0 +- 6.0 +before_install: +- npm install -g mocha install: - npm install script: diff --git a/gulp/tasks.js b/gulp/tasks.js index f87a146d57..bab78463d9 100644 --- a/gulp/tasks.js +++ b/gulp/tasks.js @@ -6,6 +6,7 @@ var gulp = require('gulp') var gutil = require('gulp-util') +var mocha = require('gulp-mocha') var runSequence = require('run-sequence') gulp.task('default', function (done) { @@ -36,6 +37,13 @@ gulp.task('watch', function (done) { }) gulp.task('test', function (done) { - gutil.log('Test that the app runs') - done() + runSequence('generate-assets', + 'mocha', + done) +}) + +gulp.task('mocha', function () { + return gulp.src(['test/**/*.js'], { read: false }) + .pipe(mocha({ reporter: 'spec' })) + .on('error', gutil.log) }) diff --git a/package.json b/package.json index 905c5800f9..11175a7082 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "scripts": { "start": "node start.js", "lint": "standard", - "test": "npm run lint" + "test": "mocha && npm run lint" }, "dependencies": { "basic-auth": "^1.0.3", @@ -42,5 +42,9 @@ "serve-favicon": "2.3.0", "standard": "^7.1.2", "sync-request": "^3.0.1" + }, + "devDependencies": { + "gulp-mocha": "^3.0.1", + "supertest": "^2.0.0" } } diff --git a/server.js b/server.js index 9f4176c1aa..010334a5b9 100644 --- a/server.js +++ b/server.js @@ -206,3 +206,5 @@ utils.findAvailablePort(app, function (port) { }) } }) + +module.exports = app diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 0000000000..4a52320178 --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1 @@ +--recursive diff --git a/test/spec/sanity-checks.js b/test/spec/sanity-checks.js new file mode 100644 index 0000000000..d377806355 --- /dev/null +++ b/test/spec/sanity-checks.js @@ -0,0 +1,32 @@ +/* global describe, it, before */ +var request = require('supertest') +var assert = require('assert') +var path = require('path') +var fs = require('fs') + +/** + * Basic sanity checks on the dev server + */ +describe('The prototype kit', function () { + var app + + before(function (done) { + app = require('../../server') + done() + }) + + it('should generate assets into the /public folder', function () { + assert.doesNotThrow(function () { + fs.accessSync(path.resolve(__dirname, '../../public/javascripts/application.js')) + fs.accessSync(path.resolve(__dirname, '../../public/images/favicon.ico')) + fs.accessSync(path.resolve(__dirname, '../../public/stylesheets/application.css')) + }) + }) + + it('should send with a well formed response for the index page', function (done) { + request(app) + .get('/') + .expect('Content-Type', /text\/html/) + .expect(200, done) + }) +}) From 64efd4a7973db0938ad1c358236ef66d57a1b122 Mon Sep 17 00:00:00 2001 From: Joe Lanman Date: Thu, 20 Oct 2016 19:28:36 +0100 Subject: [PATCH 2/4] make mocha exit properly --- gulp/tasks.js | 13 ++++++++----- package.json | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/gulp/tasks.js b/gulp/tasks.js index bab78463d9..08960d7a96 100644 --- a/gulp/tasks.js +++ b/gulp/tasks.js @@ -5,7 +5,6 @@ */ var gulp = require('gulp') -var gutil = require('gulp-util') var mocha = require('gulp-mocha') var runSequence = require('run-sequence') @@ -36,14 +35,18 @@ gulp.task('watch', function (done) { 'watch-assets', done) }) -gulp.task('test', function (done) { +gulp.task('test', function () { runSequence('generate-assets', - 'mocha', - done) + 'mocha') }) gulp.task('mocha', function () { return gulp.src(['test/**/*.js'], { read: false }) .pipe(mocha({ reporter: 'spec' })) - .on('error', gutil.log) + .once('error', () => { + process.exit(1) + }) + .once('end', () => { + process.exit() + }) }) diff --git a/package.json b/package.json index 11175a7082..5d46579ab2 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "scripts": { "start": "node start.js", "lint": "standard", - "test": "mocha && npm run lint" + "test": "gulp test && npm run lint" }, "dependencies": { "basic-auth": "^1.0.3", From 3bbbfe5faebd901c4a80da76a2aebc29bb6331c5 Mon Sep 17 00:00:00 2001 From: Gemma Leigh Date: Wed, 16 Nov 2016 12:35:20 +0000 Subject: [PATCH 3/4] As npm test runs gulp test, remove this from travis.yml --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 937532a592..ecd656aca9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,6 @@ before_install: install: - npm install script: -- gulp test - npm test after_success: - "./create-release-tag.sh" From 49965cc87d2f9ebf8ce7b0601845533b4721ecff Mon Sep 17 00:00:00 2001 From: Gemma Leigh Date: Wed, 16 Nov 2016 12:49:28 +0000 Subject: [PATCH 4/4] Simplify the sanity test check App doesn't need to listen in the test script as supertest accepts the app variable and handles the listening and un-listening itself. This also removes the needs for the after block to stop the server. --- test/spec/sanity-checks.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/test/spec/sanity-checks.js b/test/spec/sanity-checks.js index d377806355..397087bc32 100644 --- a/test/spec/sanity-checks.js +++ b/test/spec/sanity-checks.js @@ -1,20 +1,14 @@ -/* global describe, it, before */ +/* eslint-env mocha */ var request = require('supertest') -var assert = require('assert') +var app = require('../../server.js') var path = require('path') var fs = require('fs') +var assert = require('assert') /** * Basic sanity checks on the dev server */ describe('The prototype kit', function () { - var app - - before(function (done) { - app = require('../../server') - done() - }) - it('should generate assets into the /public folder', function () { assert.doesNotThrow(function () { fs.accessSync(path.resolve(__dirname, '../../public/javascripts/application.js')) @@ -27,6 +21,13 @@ describe('The prototype kit', function () { request(app) .get('/') .expect('Content-Type', /text\/html/) - .expect(200, done) + .expect(200) + .end(function (err, res) { + if (err) { + done(err) + } else { + done() + } + }) }) })