Skip to content

Instantly share code, notes, and snippets.

@Nautigsam
Created March 4, 2020 12:47
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 Nautigsam/94cba19dcb7f7ad806c87cb410077b94 to your computer and use it in GitHub Desktop.
Save Nautigsam/94cba19dcb7f7ad806c87cb410077b94 to your computer and use it in GitHub Desktop.
Use case where it could be useful to explicit how to handle an error thrown with a Sinon stub
const o = {
edit(cb) {
console.log('EDIT')
return Promise.resolve(false)
.then(cb)
}
}
function run() {
console.log('RUN')
return o.edit(fail => {
console.log('EDIT-CB')
if (fail) {
throw new Error('AAA')
}
}).catch(e => {
console.log('CATCH')
console.error(e)
})
}
const sinon = require('sinon')
const s = sinon.stub(o, 'edit').yields(true).resolves()
run()
@Nautigsam
Copy link
Author

Here I want to stub o.edit function and ensure that if an error is thrown, it is properly caught by the catch block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment