From 669a701a208381ccc0126bd70d1b9dd2ab85ea39 Mon Sep 17 00:00:00 2001 From: Scott Carver Date: Fri, 9 Nov 2018 17:26:07 -0800 Subject: [PATCH] Add basic http smoke test - Add mocha dev dependency - Add test to launch server and GET basic urls - Add `yarn test` to travis --- .travis.yml | 1 + package.json | 4 ++- test/server_process.js | 73 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 test/server_process.js diff --git a/.travis.yml b/.travis.yml index 6409518..575e07a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,3 +11,4 @@ install: - yarn script: - yarn build + - yarn test diff --git a/package.json b/package.json index 34953ed..6725624 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "private": true, "scripts": { "dev": "webpack-dev-server --content-base static --mode development", - "build": "NODE_ENV=production webpack --mode production" + "build": "NODE_ENV=production webpack --mode production", + "test": "./node_modules/mocha/bin/mocha" }, "dependencies": { "babel-polyfill": "^6.26.0", @@ -37,6 +38,7 @@ "file-loader": "^2.0.0", "html-webpack-plugin": "^3.2.0", "mini-css-extract-plugin": "^0.4.4", + "mocha": "^5.2.0", "node-sass": "^4.9.4", "redux-devtools": "^3.4.0", "sass-loader": "^7.1.0", diff --git a/test/server_process.js b/test/server_process.js new file mode 100644 index 0000000..f05fdcd --- /dev/null +++ b/test/server_process.js @@ -0,0 +1,73 @@ +var assert = require('assert'); +var child_process = require('child_process') +var http = require('http'); + +var SERVER_LAUNCH_WAIT_TIME = 5 * 1000; + +describe('server process', function() { + var server_proc = null; + var server_exited = false; + + before(function() { + this.timeout(SERVER_LAUNCH_WAIT_TIME + 1000); + + console.log("launching server...") + server_proc = child_process.spawn('yarn', ['dev'], { + cwd: '.' + }); + + server_proc.on('exit', function(code, signal) { + server_exited = true; + }); + + return (new Promise(function(done) { + // @TODO Better way to detect server alive-ness than waiting? + setTimeout(done, SERVER_LAUNCH_WAIT_TIME) + })); + }); + + after(function() { + console.log("killing server...") + server_proc.kill(); + }); + + it('should launch', function() { + assert.equal(server_exited, false); + }); + + var urls = [ + '/', + 'js/index.bundle.js' + ]; + + urls.forEach(function(url) { + + it('should respond to request for "' + url + '"', function(done) { + + http.get({ + hostname: 'localhost', + port: 8080, + path: '/' + }, function(res) { + var result_data = ''; + + if(res.statusCode != 200) { + throw new Error('Server response was not 200.'); + } + + res.on('data', function(data) { result_data += data }); + + res.on('end', function() { + if (result_data.length > 0) { + done(); + } else { + done(new Error("Server returned no data.")); + } + }); + }) + + }); + + }); + +});