Skip to content

Commit

Permalink
Merge pull request #33 from mcollina/close-function-or-undefined
Browse files Browse the repository at this point in the history
close must receive a function or a falsey value
  • Loading branch information
mcollina authored Nov 10, 2017
2 parents 2b86297 + a2f9641 commit c2172a1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
15 changes: 15 additions & 0 deletions boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,33 @@ function wrap (server, opts, instance) {
}

server[afterKey] = function (func) {
if (typeof func !== 'function') {
throw new Error('not a function')
}
instance.after(encapsulateThreeParam(func, this))
return this
}

server[readyKey] = function (func) {
if (typeof func !== 'function') {
throw new Error('not a function')
}
instance.ready(encapsulateThreeParam(func, this))
return this
}

server[onCloseKey] = function (func) {
if (typeof func !== 'function') {
throw new Error('not a function')
}
instance.onClose(encapsulateTwoParam(func, this))
return this
}

server[closeKey] = function (func) {
if (func && typeof func !== 'function') {
throw new Error('not a function')
}
instance.close(encapsulateThreeParam(func, this))
return this
}
Expand Down Expand Up @@ -207,6 +219,9 @@ Boot.prototype.onClose = function (func) {
Boot.prototype.close = function (func) {
this._error = null
if (func) {
if (typeof func !== 'function') {
throw new Error('not a function')
}
this._closeQ.push(func)
this._thereIsCloseCb = true
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "avvio",
"version": "3.0.0",
"version": "3.1.0",
"description": "Asynchronous bootstrapping of Node applications",
"main": "boot.js",
"scripts": {
Expand Down Expand Up @@ -30,10 +30,10 @@
},
"homepage": "https://github.com/mcollina/avvio#readme",
"devDependencies": {
"express": "^4.15.4",
"express": "^4.16.2",
"pre-commit": "^1.2.2",
"standard": "^10.0.3",
"tap": "^10.7.2",
"tap": "^10.7.3",
"then-sleep": "^1.0.1"
},
"dependencies": {
Expand Down
40 changes: 40 additions & 0 deletions test/close.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,43 @@ test('close without a cb', (t) => {

app.close()
})

test('close passing not a function', (t) => {
t.plan(1)

const app = boot()

app.onClose((instance, done) => {
t.ok('called')
done()
})

t.throws(() => app.close({}), { message: 'not a function' })
})

test('close passing not a function', (t) => {
t.plan(1)

const app = boot()

app.onClose((instance, done) => {
t.ok('called')
done()
})

t.throws(() => app.close({}), { message: 'not a function' })
})

test('close passing not a function when wrapping', (t) => {
t.plan(1)

const app = {}
boot(app)

app.onClose((instance, done) => {
t.ok('called')
done()
})

t.throws(() => app.close({}), { message: 'not a function' })
})

0 comments on commit c2172a1

Please sign in to comment.