From 057b8885edefb21fae4e11c65b1ba1a6fc0799a0 Mon Sep 17 00:00:00 2001 From: "greenkeeper[bot]" Date: Thu, 19 Oct 2017 11:09:46 -0400 Subject: [PATCH] Clean up after userApp() and upgrade to the latest Mocha (#72) See mochajs/mocha#2879 to understand how these are linked. --- lib/icedfrisby.js | 51 +++++++++++++++++++++++++++++++++---------- package.json | 2 +- test/frisby_useApp.js | 3 ++- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/lib/icedfrisby.js b/lib/icedfrisby.js index 425c05e4..8b647af9 100644 --- a/lib/icedfrisby.js +++ b/lib/icedfrisby.js @@ -104,21 +104,21 @@ function _jsonParse(body) { /** * @param {string} app - Node.js app. (e.g. Express, Koa) * @param {string} basePath - base path to append to the server address - * @return {string} - * @desc Build the baseUri from the application, replacing any existing baseUri + * @return {Object} */ -function _useAppImpl (app, basePath) { - // sanity check app - if (!app) { throw new Error('No app provided') } - +function startApp (app, basePath) { // coerce basePath to a string basePath = basePath ? basePath + '' : '' - const address = app.listen().address() - const port = address.port + const server = app.listen() + const protocol = app instanceof https.Server ? 'https' : 'http' + const port = server.address().port - return protocol + '://127.0.0.1:' + port + basePath + return { + server, + uri: `${protocol}://127.0.0.1:${port}${basePath}`, + } } @@ -159,6 +159,9 @@ class Frisby { retry_backoff: clone.retry_backoff || 1000, failures: [], + // For an app provided to a single test. + app: null, + // Custom vars added to test HTTP Request (like headers) request: clone.request, @@ -194,8 +197,12 @@ class Frisby { _initDefined = true _init = _.merge(_.cloneDeep(_icedfrisbyDefaults()), obj) - if(_init.useApp) { - _init.request.baseUri = _useAppImpl(_init.useApp) + if (obj.useApp) { + const { server, uri } = startApp(_init.useApp) + after(function (done) { + server.close(() => { done() }) + }) + _init.request.baseUri = uri } } return _init @@ -229,7 +236,11 @@ class Frisby { * @desc Setup the request to use a passed in Node http.Server app */ useApp (app, basePath) { - this.current.request.baseUri = _useAppImpl(app, basePath) + if (!app) { throw new Error('No app provided') } + this.current.useApp = { app, basePath } + // The app's baseUri won't be known until the app is started. Its uri + // will be prepended then. + this.current.request.baseUri = null return this } @@ -1156,6 +1167,22 @@ class Frisby { const describeFn = this._only ? describe.only : describe describeFn(self.current.describe, function () { + before(function () { + if (self.current.useApp) { + const { app, baseUri } = self.current.useApp + const { server, uri } = startApp(app, baseUri) + self.current.server = server + self.current.outgoing.uri = uri + self.current.outgoing.uri + } + }) + after(function (done) { + if (self.current.server) { + self.current.server.close(() => { done() }) + } else { + done() + } + }) + it("\n\t[ " + self.current.itInfo + " ]", function (done) { self._start(done) }) diff --git a/package.json b/package.json index e262beb9..5dbd5d27 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "express": "^4.14.0", "intercept-stdout": "^0.1.2", "istanbul": "^0.4.5", - "mocha": "^3.5.3", + "mocha": "^4.0.1", "mocha-lcov-reporter": "^1.2.0", "mock-request": "^0.1.2", "nock": "^9.0.2", diff --git a/test/frisby_useApp.js b/test/frisby_useApp.js index 0dc3a4d7..e597b198 100644 --- a/test/frisby_useApp.js +++ b/test/frisby_useApp.js @@ -38,12 +38,13 @@ describe('IcedFrisby useApp(app)', function() { res.send('^.^') }) - app.listen(4000, function() { + const server = app.listen(4000, () => { frisby.create(this.test.title) .useApp(app) .get('/') .expectStatus(200) .expectBodyContains('^.^') + .after(() => { server.close() }) .toss() }) })