Skip to content

Instantly share code, notes, and snippets.

@weiting-tw
Forked from haroldtreen/promise-test.js
Created August 1, 2018 08:45
Show Gist options
  • Save weiting-tw/2a3a026bec55f0f1473d5c212f93dae3 to your computer and use it in GitHub Desktop.
Save weiting-tw/2a3a026bec55f0f1473d5c212f93dae3 to your computer and use it in GitHub Desktop.
Testing promise rejection with Mocha
const { assert } = require('chai');
function isError(e) {
if (typeof e === 'string') {
return Promise.reject(new Error(e));
}
return Promise.resolve(e);
}
describe('Promise Testing', () => {
// With done callback - 8 lines
it('should test rejections using done', (done) => {
aMethodThatRejects()
.then(() => {
done(new Error('Expected method to reject.'))
})
.catch((err) => {
assert.isDefined(err);
done();
})
.catch(done);
});
// With built in Promise support - 8 lines + helper
it('should test rejections using catch', () => {
return aMethodThatRejects()
.then(() => {
return Promise.reject('Expected method to reject.');
})
.catch(isError)
.then((err) => {
assert.isDefined(err);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment