Skip to content

Commit

Permalink
Clean up after userApp() and upgrade to the latest Mocha (#72)
Browse files Browse the repository at this point in the history
See mochajs/mocha#2879 to understand how these are linked.
  • Loading branch information
greenkeeper[bot] authored and paulmelnikow committed Oct 19, 2017
1 parent a171ebd commit 057b888
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
51 changes: 39 additions & 12 deletions lib/icedfrisby.js
Expand Up @@ -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}`,
}
}


Expand Down Expand Up @@ -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,

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion test/frisby_useApp.js
Expand Up @@ -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()
})
})
Expand Down

0 comments on commit 057b888

Please sign in to comment.