Skip to content

Instantly share code, notes, and snippets.

@felixvisee
Created July 4, 2013 16:00
Show Gist options
  • Save felixvisee/5928809 to your computer and use it in GitHub Desktop.
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
describe("Something", function () {
it("should promise", promising(function () {
return Promise.reject("Ouch!");
}));
});
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