leverage ava's async features to ensure order in server_process tests

This commit is contained in:
Lachlan Kermode
2018-12-14 17:50:00 +00:00
parent 95a501aba2
commit 18ddc6c48b
2 changed files with 65 additions and 95 deletions

View File

@@ -1,51 +1,71 @@
var assert = require('assert');
var child_process = require('child_process')
var http = require('http');
import test from 'ava'
import fetch from 'node-fetch'
import child_process from 'child_process'
const SERVER_LAUNCH_WAIT_TIME = 10 * 1000;
var server_proc = null;
var server_exited = false;
const SERVER_LAUNCH_WAIT_TIME = 10 * 1000
const SERVER_ROOT = 'http://localhost:4040'
let server_proc = null
let server_exited = false
function checkStatus(res) {
if (res.ok) {
return res
} else {
throw new Error('Route is not present')
}
}
/* SETUP: launch a development server with a wait time */
test.before.cb(t => {
console.log("launching server...")
server_proc = child_process.spawn('yarn', ['dev'], {
cwd: '.',
stdio: 'ignore'
});
})
server_proc.on('exit', function(code, signal) {
server_exited = true;
});
server_exited = true
})
setTimeout(t.end, SERVER_LAUNCH_WAIT_TIME);
});
setTimeout(t.end, SERVER_LAUNCH_WAIT_TIME)
})
/* CLEANUP: kill the server */
test.after(function() {
console.log("killing server...")
server_proc.kill('SIGKILL');
});
server_proc.kill('SIGKILL')
})
test('should launch', t => {
t.false(server_exited);
});
t.false(server_exited)
})
var passUrls = [
// Express API registered routes:
test('should update', t => {
const expected = {
success: "All sheets updated"
}
return fetch(`${SERVER_ROOT}/api/update`)
.then(checkStatus)
.then(res => res.json())
.then(json => {
t.deepEqual(json, expected)
})
})
const passUrls = [
// /
'/api/',
// /blueprints
'/api/blueprints',
// /update
'/api/update',
// /:sheet/:tab/:resource
'/api/example/export_events/rows',
// /:sheet/:tab/:resource/:frag
'/api/example/export_events/rows/1'
];
]
var failUrls = [
const failUrls = [
// /:sheet
'/api/example',
// /:sheet/:tab
@@ -53,51 +73,25 @@ var failUrls = [
]
passUrls.forEach(function(url) {
test.cb('should respond successfully to request for "' + url + '"', t => {
http.get({
hostname: 'localhost',
port: 4040,
path: url,
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) {
t.pass();
} else {
t.fail("Server returned no data.");
}
});
}
t.end();
})
});
});
test(`should respond successfully to request for ${url}`, t => {
return fetch(`${SERVER_ROOT}${url}`)
.then(checkStatus)
.then(res => res.json())
.then(json => {
t.pass()
})
})
})
failUrls.forEach(function(url) {
test.cb('should fail to respond to request for "' + url + '"', t => {
http.get({
hostname: 'localhost',
port: 4040,
path: url,
agent: false
}, function(res) {
var result_data = '';
if(res.statusCode < 400) {
t.fail('Server response was not erroneous.');
} else {
t.pass();
}
t.end();
})
});
});
test(`should respond with 404 for ${url}`, t => {
return fetch(`${SERVER_ROOT}${url}`)
.then(res => {
if (!res.ok) {
t.pass()
} else {
t.fail()
}
})
})
})

View File

@@ -999,7 +999,6 @@ call-signature@0.0.2:
caller-callsite@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/caller-callsite/-/caller-callsite-2.0.0.tgz#847e0fce0a223750a9a027c54b33731ad3154134"
integrity sha1-hH4PzgoiN1CpoCfFSzNzGtMVQTQ=
dependencies:
callsites "^2.0.0"
@@ -1012,7 +1011,6 @@ caller-path@^0.1.0:
caller-path@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/caller-path/-/caller-path-2.0.0.tgz#468f83044e369ab2010fac5f06ceee15bb2cb1f4"
integrity sha1-Ro+DBE42mrIBD6xfBs7uFbsssfQ=
dependencies:
caller-callsite "^2.0.0"
@@ -1023,7 +1021,6 @@ callsites@^0.2.0:
callsites@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/callsites/-/callsites-2.0.0.tgz#06eb84f00eea413da86affefacbffb36093b3c50"
integrity sha1-BuuE8A7qQT2oav/vrL/7Ngk7PFA=
camelcase-keys@^4.0.0:
version "4.2.0"
@@ -1296,7 +1293,6 @@ cors@^2.8.5:
cosmiconfig@^5.0.6:
version "5.0.7"
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-5.0.7.tgz#39826b292ee0d78eda137dfa3173bd1c21a43b04"
integrity sha512-PcLqxTKiDmNT6pSpy4N6KtuPwb53W+2tzNvwOZw0WH9N6O0vLIBq0x8aj8Oj75ere4YcGi48bDFCL+3fRJdlNA==
dependencies:
import-fresh "^2.0.0"
is-directory "^0.3.1"
@@ -1502,7 +1498,6 @@ dot-prop@^4.1.0, dot-prop@^4.2.0:
dotenv@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-6.1.0.tgz#9853b6ca98292acb7dec67a95018fa40bccff42c"
integrity sha512-/veDn2ztgRlB7gKmE3i9f6CmDIyXAy6d5nBq+whO9SLX+Zs1sXEgFLPi+aSuWqUuusMfbi84fT8j34fs1HaYUw==
duplexer3@^0.1.4:
version "0.1.4"
@@ -1540,7 +1535,6 @@ encodeurl@~1.0.2:
end-of-stream@^1.1.0:
version "1.4.1"
resolved "https://registry.yarnpkg.com/end-of-stream/-/end-of-stream-1.4.1.tgz#ed29634d19baba463b6ce6b80a37213eab71ec43"
integrity sha512-1MkrZNvWTKCaigbn+W15elq2BB/L22nqrSY5DKlo3X6+vclJm8Bb5djXJBmEX6fS3+zCh/F4VBK5Z2KxJt4s2Q==
dependencies:
once "^1.4.0"
@@ -1785,7 +1779,6 @@ execa@^0.7.0:
execa@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/execa/-/execa-1.0.0.tgz#c6236a5bb4df6d6f15e88e7f017798216749ddd8"
integrity sha512-adbxcyWV46qiHyvSp50TKt05tB4tK3HcmF7/nxfAdhnox83seTDbwnaqKO4sXRy7roHAIFqJP/Rw/AuEbX61LA==
dependencies:
cross-spawn "^6.0.0"
get-stream "^4.0.0"
@@ -1967,7 +1960,6 @@ find-up@^2.0.0, find-up@^2.1.0:
find-up@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/find-up/-/find-up-3.0.0.tgz#49169f1d7993430646da61ecc5ae355c21c97b73"
integrity sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==
dependencies:
locate-path "^3.0.0"
@@ -2073,7 +2065,6 @@ get-stream@^3.0.0:
get-stream@^4.0.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-4.1.0.tgz#c1b255575f3dc21d59bfc79cd3d2b46b1c3a54b5"
integrity sha512-GMat4EJ5161kIy2HevLlr4luNjBgvmj413KaQA7jt4V8B4RDsfpHk7WQ9GVqfYyyx8OS/L66Kox+rJRNklLK7w==
dependencies:
pump "^3.0.0"
@@ -2297,7 +2288,6 @@ http-errors@^1.3.0:
husky@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/husky/-/husky-1.2.0.tgz#d631dda1e4a9ee8ba69a10b0c51a0e2c66e711e5"
integrity sha512-/ib3+iycykXC0tYIxsyqierikVa9DA2DrT32UEirqNEFVqOj1bFMTgP3jAz8HM7FgC/C8pc/BTUa9MV2GEkZaA==
dependencies:
cosmiconfig "^5.0.6"
execa "^1.0.0"
@@ -2343,7 +2333,6 @@ ignore@^4.0.2:
import-fresh@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-2.0.0.tgz#d81355c15612d386c61f9ddd3922d4304822a546"
integrity sha1-2BNVwVYS04bGH53dOSLUMEgipUY=
dependencies:
caller-path "^2.0.0"
resolve-from "^3.0.0"
@@ -2491,7 +2480,6 @@ is-descriptor@^1.0.0, is-descriptor@^1.0.2:
is-directory@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/is-directory/-/is-directory-0.3.1.tgz#61339b6f2475fc772fd9c9d83f5c8575dc154ae1"
integrity sha1-YTObbyR1/Hcv2cnYP1yFddwVSuE=
is-error@^2.2.1:
version "2.2.1"
@@ -2787,7 +2775,6 @@ locate-path@^2.0.0:
locate-path@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-3.0.0.tgz#dbec3b3ab759758071b58fe59fc41871af21400e"
integrity sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==
dependencies:
p-locate "^3.0.0"
path-exists "^3.0.0"
@@ -3094,9 +3081,9 @@ nice-try@^1.0.4:
version "1.0.5"
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
node-fetch@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.2.0.tgz#4ee79bde909262f9775f731e3656d0db55ced5b5"
node-fetch@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.3.0.tgz#1a1d940bbfb916a1d3e0219f037e89e71f8c5fa5"
node-forge@^0.7.4:
version "0.7.6"
@@ -3130,7 +3117,6 @@ node-releases@^1.0.0-alpha.14:
nodemon@1.18.7:
version "1.18.7"
resolved "https://registry.yarnpkg.com/nodemon/-/nodemon-1.18.7.tgz#716b66bf3e89ac4fcfb38a9e61887a03fc82efbb"
integrity sha512-xuC1V0F5EcEyKQ1VhHYD13owznQbUw29JKvZ8bVH7TmuvVNHvvbp9pLgE4PjTMRJVe0pJ8fGRvwR2nMiosIsPQ==
dependencies:
chokidar "^2.0.4"
debug "^3.1.0"
@@ -3320,7 +3306,6 @@ p-limit@^1.1.0:
p-limit@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-2.0.0.tgz#e624ed54ee8c460a778b3c9f3670496ff8a57aec"
integrity sha512-fl5s52lI5ahKCernzzIyAP0QAZbGIovtVHGwpcu1Jr/EpzLVDI2myISHwGqK7m8uQFugVWSrbxH7XnhGtvEc+A==
dependencies:
p-try "^2.0.0"
@@ -3333,7 +3318,6 @@ p-locate@^2.0.0:
p-locate@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-3.0.0.tgz#322d69a05c0264b25997d9f40cd8a891ab0064a4"
integrity sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==
dependencies:
p-limit "^2.0.0"
@@ -3348,7 +3332,6 @@ p-try@^1.0.0:
p-try@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.0.0.tgz#85080bb87c64688fa47996fe8f7dfbe8211760b1"
integrity sha512-hMp0onDKIajHfIkdRk3P4CdCmErkYAxxDtP3Wx/4nZ3aGlau2VKh3mZpcuFkH27WQkL/3WBCPOktzA9ZOAnMQQ==
package-hash@^2.0.0:
version "2.0.0"
@@ -3497,14 +3480,12 @@ pkg-dir@^2.0.0:
pkg-dir@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/pkg-dir/-/pkg-dir-3.0.0.tgz#2749020f239ed990881b1f71210d51eb6523bea3"
integrity sha512-/E57AYkoeQ25qkxMj5PBOVgF8Kiu/h7cYS30Z5+R7WaiCCBfLq58ZI/dSeaEKb9WVJV5n/03QwrN3IeWIFllvw==
dependencies:
find-up "^3.0.0"
please-upgrade-node@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/please-upgrade-node/-/please-upgrade-node-3.1.1.tgz#ed320051dfcc5024fae696712c8288993595e8ac"
integrity sha512-KY1uHnQ2NlQHqIJQpnh/i54rKkuxCEBx+voJIS/Mvb+L2iYd2NMotwduhKTMjfC1uKoX3VXOxLjIYG66dfJTVQ==
dependencies:
semver-compare "^1.0.0"
@@ -3569,12 +3550,10 @@ pseudomap@^1.0.2:
pstree.remy@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/pstree.remy/-/pstree.remy-1.1.2.tgz#4448bbeb4b2af1fed242afc8dc7416a6f504951a"
integrity sha512-vL6NLxNHzkNTjGJUpMm5PLC+94/0tTlC1vkP9bdU0pOHih+EujMjgMTwfZopZvHWRFbqJ5Y73OMoau50PewDDA==
pump@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/pump/-/pump-3.0.0.tgz#b4a2116815bde2f4e1ea602354e8c75565107a64"
integrity sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==
dependencies:
end-of-stream "^1.1.0"
once "^1.3.1"
@@ -3650,7 +3629,6 @@ read-pkg@^3.0.0:
read-pkg@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/read-pkg/-/read-pkg-4.0.1.tgz#963625378f3e1c4d48c85872b5a6ec7d5d093237"
integrity sha1-ljYlN48+HE1IyFhytabsfV0JMjc=
dependencies:
normalize-package-data "^2.3.2"
parse-json "^4.0.0"
@@ -3835,7 +3813,6 @@ run-async@^2.2.0:
run-node@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/run-node/-/run-node-1.0.0.tgz#46b50b946a2aa2d4947ae1d886e9856fd9cabe5e"
integrity sha512-kc120TBlQ3mih1LSzdAJXo4xn/GWS2ec0l3S+syHDXP9uRr0JAT8Qd3mdMuyjqCzeZktgP3try92cEgf9Nks8A==
run-parallel@^1.1.2:
version "1.1.9"
@@ -3868,7 +3845,6 @@ sax@^1.2.4:
semver-compare@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/semver-compare/-/semver-compare-1.0.0.tgz#0dee216a1c941ab37e9efb1788f6afc5ff5537fc"
integrity sha1-De4hahyUGrN+nvsXiPavxf9VN/w=
semver-diff@^2.0.0:
version "2.1.0"