Created
December 4, 2017 17:44
-
-
Save edwinwright/bf747d522cc14144cbeed4fcd9fd8e7b to your computer and use it in GitHub Desktop.
Testing functions that return Promises in Mocha
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
// Basic verification | |
// Returns the promise from somethingAsync() | |
// The test passes if resolved, fails if rejected | |
it('does something async with promises', function() { | |
return somethingAsync(); | |
}); | |
// Verify a fulfilled promise | |
// Returns the promise from somethingAsync() and runs assertions in the onFulfilled callback | |
// If an assertion fails, a rejected promise is returned and the test fails | |
it('does something async with promises', function() { | |
return somethingAsync().then(function fulfilled(result) { | |
assert(...) | |
}); | |
}); | |
// Verify a rejected promise | |
// Returns the promise from somethingAsync() and runs assertions in the onRejected callback | |
// If the promise resolves, an error is thrown and the test fails | |
// If an assertion fails, a rejected promise is returned and the test fails | |
it('returns rejected promise on bad input', function() { | |
return somethingAsync('bad input').then(function fulfilled(result) { | |
throw new Error('Promise was unexpectedly fulfilled. Result: ' + result); | |
}, function rejected(error) { | |
assert(...) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment