Skip to content

Instantly share code, notes, and snippets.

@asalant
Created November 26, 2011 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asalant/1394976 to your computer and use it in GitHub Desktop.
Save asalant/1394976 to your computer and use it in GitHub Desktop.
Provide better asynchronous support for jasmine-node
# Monkey patch jasmine.Env to wrap spec ('it', 'beforeEach', 'afterEach')
# in async handler if it expects a done callback
withoutAsync = {}
for jasmineFunction in [ "it", "beforeEach", "afterEach"]
do (jasmineFunction) ->
withoutAsync[jasmineFunction] = jasmine.Env.prototype[jasmineFunction]
jasmine.Env.prototype[jasmineFunction] = (args...) ->
specFunction = args.pop()
# No async callback expected, so not async
if specFunction.length == 0
args.push specFunction
else
args.push -> asyncSpec(specFunction, @)
withoutAsync[jasmineFunction].apply @, args
# Run any function, failing the current spec if there is an error or it times out
asyncSpec = (specFunction, spec, timeout = 1000) ->
done = false
spec.runs ->
try
specFunction (error) ->
done = true
spec.fail(error) if error?
catch e
# if we hit an exception before any async code, mark the spec done
done = true
throw e
spec.waitsFor ->
done == true
, "spec to complete", timeout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment