Skip to content

Commit

Permalink
fix: support old nodejs without Promise.prototype.finally
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed Mar 19, 2021
1 parent 3327f80 commit fb70713
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions lib/bypass.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,17 @@ exports = module.exports = function bypass(fn) {
try {
result = fn();
} finally {
if (result && typeof result.finally === 'function') {
result.finally(enable);
if (result && typeof result.then === 'function') {
result.then(
r => {
enable();
return r;
},
err => {
enable();
throw err;
}
);
} else {
enable();
}
Expand Down
2 changes: 1 addition & 1 deletion test/lib/bypass.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('mock.bypass()', () => {

promise
.then(() => {
done(new Error('expected rejection'));
done(new Error('should not succeed'));
})
.catch(err => {
assert.equal(err, error);
Expand Down

0 comments on commit fb70713

Please sign in to comment.