From 669a701a208381ccc0126bd70d1b9dd2ab85ea39 Mon Sep 17 00:00:00 2001 From: Scott Carver Date: Fri, 9 Nov 2018 17:26:07 -0800 Subject: [PATCH 1/9] 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.")); + } + }); + }) + + }); + + }); + +}); From d0259756a57c6c2cac4ce9b5869a5df09a8da359 Mon Sep 17 00:00:00 2001 From: Scott Carver Date: Fri, 9 Nov 2018 17:29:57 -0800 Subject: [PATCH 2/9] Increase GET test timeout Tests are not currently passing on travis, probably because of slower machine. --- test/server_process.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/server_process.js b/test/server_process.js index f05fdcd..ec58a47 100644 --- a/test/server_process.js +++ b/test/server_process.js @@ -43,6 +43,7 @@ describe('server process', function() { urls.forEach(function(url) { it('should respond to request for "' + url + '"', function(done) { + this.timeout(5000); http.get({ hostname: 'localhost', From 9b69feff2c4d097a6dedd387e80cb6e7c694ca3a Mon Sep 17 00:00:00 2001 From: Scott Carver Date: Fri, 9 Nov 2018 17:51:21 -0800 Subject: [PATCH 3/9] Use bash for shell This may work around a problem where kill() does not work correctly in a travis context (https://github.com/travis-ci/travis-ci/issues/704) --- test/server_process.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/server_process.js b/test/server_process.js index ec58a47..0a98845 100644 --- a/test/server_process.js +++ b/test/server_process.js @@ -13,7 +13,8 @@ describe('server process', function() { console.log("launching server...") server_proc = child_process.spawn('yarn', ['dev'], { - cwd: '.' + cwd: '.', + shell: '/bin/bash' }); server_proc.on('exit', function(code, signal) { From 08b3543b87008d5b606404d296b7e0e402a8c34f Mon Sep 17 00:00:00 2001 From: Scott Carver Date: Fri, 9 Nov 2018 18:08:42 -0800 Subject: [PATCH 4/9] Fix travis subprocess.kill problem - attempt 2 --- test/server_process.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/server_process.js b/test/server_process.js index 0a98845..21e1ee4 100644 --- a/test/server_process.js +++ b/test/server_process.js @@ -29,6 +29,7 @@ describe('server process', function() { after(function() { console.log("killing server...") + server_proc.unref(); server_proc.kill(); }); From 3beefe09250ed61da9b6a6a084cf5b97f0725afa Mon Sep 17 00:00:00 2001 From: Scott Carver Date: Fri, 9 Nov 2018 18:24:37 -0800 Subject: [PATCH 5/9] Fix travis subprocess.kill problem - attempt 3 Spawn a detached process, which should also prevent parent from waiting for kill to finish. --- test/server_process.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/server_process.js b/test/server_process.js index 21e1ee4..5bbe981 100644 --- a/test/server_process.js +++ b/test/server_process.js @@ -14,7 +14,8 @@ describe('server process', function() { console.log("launching server...") server_proc = child_process.spawn('yarn', ['dev'], { cwd: '.', - shell: '/bin/bash' + shell: '/bin/bash', + detached: true }); server_proc.on('exit', function(code, signal) { From 1a6ec88c2db1346e68f4a1531165d53c6d538e7c Mon Sep 17 00:00:00 2001 From: Scott Carver Date: Sat, 10 Nov 2018 15:51:32 -0800 Subject: [PATCH 6/9] Fix travis subprocess.kill problem - attempt 3 - Don't connect to pipes of spawned yawn process (we don't need them, and they can prevent us from exiting the mocha process) - agent: false to avoid keeping http connection alive even after get is finished (also keeping mocha process alive) --- test/server_process.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/test/server_process.js b/test/server_process.js index 5bbe981..a77a6a5 100644 --- a/test/server_process.js +++ b/test/server_process.js @@ -14,24 +14,22 @@ describe('server process', function() { console.log("launching server...") server_proc = child_process.spawn('yarn', ['dev'], { cwd: '.', - shell: '/bin/bash', - detached: true + stdio: 'ignore' }); server_proc.on('exit', function(code, signal) { server_exited = true; }); - return (new Promise(function(done) { + return (new Promise(function(resolve) { // @TODO Better way to detect server alive-ness than waiting? - setTimeout(done, SERVER_LAUNCH_WAIT_TIME) + setTimeout(resolve, SERVER_LAUNCH_WAIT_TIME) })); }); after(function() { console.log("killing server...") - server_proc.unref(); - server_proc.kill(); + server_proc.kill('SIGKILL'); }); it('should launch', function() { @@ -51,7 +49,8 @@ describe('server process', function() { http.get({ hostname: 'localhost', port: 8080, - path: '/' + path: '/', + agent: false }, function(res) { var result_data = ''; From eccef187246cf95080b20880c241627e8ffa7bbb Mon Sep 17 00:00:00 2001 From: Scott Carver Date: Sun, 11 Nov 2018 13:26:15 -0800 Subject: [PATCH 7/9] Switch test framework to ava Should match datasheet-server. --- package.json | 8 +++- test/server_process.js | 90 +++++++++++++++++++----------------------- 2 files changed, 48 insertions(+), 50 deletions(-) diff --git a/package.json b/package.json index 6725624..4526f9a 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "scripts": { "dev": "webpack-dev-server --content-base static --mode development", "build": "NODE_ENV=production webpack --mode production", - "test": "./node_modules/mocha/bin/mocha" + "test": "npx ava --verbose" }, "dependencies": { "babel-polyfill": "^6.26.0", @@ -33,6 +33,7 @@ "@babel/core": "^7.1.2", "@babel/preset-env": "^7.1.0", "@babel/preset-react": "^7.0.0", + "ava": "1.0.0-beta.8", "babel-loader": "^8.0.4", "css-loader": "^1.0.0", "file-loader": "^2.0.0", @@ -46,5 +47,10 @@ "webpack": "^4.20.2", "webpack-cli": "^3.1.2", "webpack-dev-server": "^3.1.9" + }, + "ava": { + "files": [ + "test/**/*.js" + ] } } diff --git a/test/server_process.js b/test/server_process.js index a77a6a5..350e901 100644 --- a/test/server_process.js +++ b/test/server_process.js @@ -1,75 +1,67 @@ var assert = require('assert'); var child_process = require('child_process') var http = require('http'); +import test from 'ava' -var SERVER_LAUNCH_WAIT_TIME = 5 * 1000; +const SERVER_LAUNCH_WAIT_TIME = 5 * 1000; -describe('server process', function() { - var server_proc = null; - var server_exited = false; +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: '.', - stdio: 'ignore' - }); - - server_proc.on('exit', function(code, signal) { - server_exited = true; - }); - - return (new Promise(function(resolve) { - // @TODO Better way to detect server alive-ness than waiting? - setTimeout(resolve, SERVER_LAUNCH_WAIT_TIME) - })); +test.before.cb(t => { + console.log("launching server...") + server_proc = child_process.spawn('yarn', ['dev'], { + cwd: '.', + stdio: 'ignore' }); - after(function() { - console.log("killing server...") - server_proc.kill('SIGKILL'); + server_proc.on('exit', function(code, signal) { + server_exited = true; }); - it('should launch', function() { - assert.equal(server_exited, false); - }); + setTimeout(t.end, SERVER_LAUNCH_WAIT_TIME) +}); - var urls = [ - '/', - 'js/index.bundle.js' - ]; +test.after(function() { + console.log("killing server...") + server_proc.kill('SIGKILL'); +}); - urls.forEach(function(url) { +test('should launch', t => { + t.false(server_exited); +}); - it('should respond to request for "' + url + '"', function(done) { - this.timeout(5000); +var urls = [ + '/', + 'js/index.bundle.js' +]; - http.get({ - hostname: 'localhost', - port: 8080, - path: '/', - agent: false - }, function(res) { - var result_data = ''; - - if(res.statusCode != 200) { - throw new Error('Server response was not 200.'); - } +urls.forEach(function(url) { + test.cb('should respond to request for "' + url + '"', t => { + http.get({ + hostname: 'localhost', + port: 8080, + path: '/', + agent: false + }, function(res) { + var result_data = ''; + if(res.statusCode != 200) { + t.fail('Server response was not 200.'); + } else { res.on('data', function(data) { result_data += data }); res.on('end', function() { if (result_data.length > 0) { - done(); + t.pass(); } else { - done(new Error("Server returned no data.")); + t.fail("Server returned no data."); } }); - }) + } - }); + t.end(); + }) }); From 39f9bdc5ebfd74430db9226ba3568830412a29a1 Mon Sep 17 00:00:00 2001 From: Scott Carver Date: Sun, 11 Nov 2018 13:32:45 -0800 Subject: [PATCH 8/9] Run ava directly --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4526f9a..2ee48d1 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "scripts": { "dev": "webpack-dev-server --content-base static --mode development", "build": "NODE_ENV=production webpack --mode production", - "test": "npx ava --verbose" + "test": "ava --verbose" }, "dependencies": { "babel-polyfill": "^6.26.0", From 163fccdb022da95a51b40eaba578b7cacf9aaf87 Mon Sep 17 00:00:00 2001 From: Scott Carver Date: Sun, 11 Nov 2018 21:27:26 -0800 Subject: [PATCH 9/9] Add test-watch script --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 2ee48d1..be401e2 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "scripts": { "dev": "webpack-dev-server --content-base static --mode development", "build": "NODE_ENV=production webpack --mode production", - "test": "ava --verbose" + "test": "ava --verbose", + "test-watch": "ava --watch" }, "dependencies": { "babel-polyfill": "^6.26.0",