Skip to content

Instantly share code, notes, and snippets.

@edwinwright
Created December 4, 2017 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwinwright/bf747d522cc14144cbeed4fcd9fd8e7b to your computer and use it in GitHub Desktop.
Save edwinwright/bf747d522cc14144cbeed4fcd9fd8e7b to your computer and use it in GitHub Desktop.
Testing functions that return Promises in Mocha
// 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