Created
July 4, 2013 16:00
-
-
Save felixvisee/5928809 to your computer and use it in GitHub Desktop.
Jasmine promise-returning spec wrapper function, example with https://github.com/slightlyoff/Promises
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
describe("Something", function () { | |
it("should promise", promising(function () { | |
return Promise.reject("Ouch!"); | |
})); | |
}); |
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
function promising(fun, message, timeout) { | |
return function () { | |
var spec = jasmine.getEnv().currentSpec; | |
var completed = false; | |
runs(function () { | |
var result = fun(); | |
if (typeof result === 'undefined') { | |
completed = true; | |
} else if (typeof result !== 'object' || typeof result.then !== 'function') { | |
spec.fail(new Error("`it(promising)` block returns non-promise: " + result)); | |
completed = true; | |
} else { | |
try { | |
result.then(function (value) { | |
if (typeof value !== 'undefined') { | |
spec.fail(new Error("Promise fulfilled with unexpected value: " + value)); | |
} | |
completed = true; | |
}, function (error) { | |
spec.fail(error); | |
completed = true; | |
}); | |
} catch (error) { | |
spec.fail(error); | |
completed = true; | |
} | |
} | |
}); | |
waitsFor(function () { | |
return completed; | |
}, message, timeout); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment