Created
November 26, 2011 04:02
-
-
Save asalant/1394976 to your computer and use it in GitHub Desktop.
Provide better asynchronous support for jasmine-node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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