Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lib/hooks.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
async = require 'async'
_ = require 'underscore'

class Hooks
constructor: () ->
Expand Down Expand Up @@ -49,23 +48,30 @@ class Hooks
callback(err)

runBefore: (test, callback) =>
return callback() unless (@beforeHooks[test.name] or @beforeEachHooks)
beforeHook = @getMatchingHook @beforeHooks, test.name
return callback() unless (beforeHook or @beforeEachHooks)

hooks = @beforeEachHooks.concat(@beforeHooks[test.name] ? [])
hooks = @beforeEachHooks.concat(beforeHook ? [])
async.eachSeries hooks, (hook, callback) ->
hook test, callback
, callback

runAfter: (test, callback) =>
return callback() unless (@afterHooks[test.name] or @afterEachHooks)
afterHook = @getMatchingHook @afterHooks, test.name
return callback() unless (afterHook or @afterEachHooks)

hooks = (@afterHooks[test.name] ? []).concat(@afterEachHooks)
hooks = (afterHook ? []).concat(@afterEachHooks)
async.eachSeries hooks, (hook, callback) ->
hook test, callback
, callback

hasName: (name) =>
_.has(@beforeHooks, name) || _.has(@afterHooks, name)
(@getMatchingHook @beforeHooks, name) || (@getMatchingHook @afterHooks, name)

getMatchingHook: (hooks, name) =>
for key,value of hooks
if name.match key
return value


module.exports = new Hooks()
Expand Down
8 changes: 8 additions & 0 deletions test/unit/hooks-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ describe 'Hooks', () ->

hooks.beforeHooks = {}

it 'should return true if test name matches regular expression hook', ->
hooks.beforeHooks =
'GET /.* -> [200|404]': (test, done) ->
done()

assert.ok hooks.hasName 'GET /users -> 200'
hooks.beforeHooks = {}

it 'should return true if in after hooks', ->
hooks.afterHooks =
foo: (test, done) ->
Expand Down