Created
August 23, 2014 19:23
-
-
Save charmander/720a144b05b213591363 to your computer and use it in GitHub Desktop.
Promise-compatible tests in 36 lines.
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
var descriptions = []; | |
/* tests here */ | |
function id(x) { | |
return x; | |
} | |
function describe(described, description) { | |
descriptions.push(function () { | |
var promises = []; | |
description(function it(behaviour, test) { | |
var p = new Promise(function (resolve) { | |
resolve(test()); | |
}).catch(id); | |
p.name = behaviour; | |
promises.push(p); | |
}); | |
return Promise.all(promises).then(function (results) { | |
return results.reduce(function (allPassing, result, i) { | |
var p = promises[i]; | |
if (result instanceof Error) { | |
console.log('\x1b[31m✘\x1b[0m \x1b[1m%s %s\x1b[0m failed\n%s', described, p.name, result.stack); | |
return false; | |
} | |
console.log('\x1b[32m✔\x1b[0m \x1b[1m%s %s\x1b[0m passed', described, p.name); | |
return allPassing; | |
}, true); | |
}); | |
}); | |
} | |
descriptions.reduce(function (first, second) { | |
return first.then(function (allPassed) { | |
return second().then(function (passed) { | |
return allPassed && passed; | |
}); | |
}); | |
}, Promise.resolve(true)).done(function (allPassed) { | |
process.exit(!allPassed); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment