diff --git a/lib/bypass.js b/lib/bypass.js index 4d6b29c..a65aada 100755 --- a/lib/bypass.js +++ b/lib/bypass.js @@ -18,9 +18,8 @@ exports = module.exports = function bypass(fn) { let result; try { result = fn(); - } finally { if (result && typeof result.then === 'function') { - result.then( + return result.then( r => { enable(); return r; @@ -32,10 +31,12 @@ exports = module.exports = function bypass(fn) { ); } else { enable(); + return result; } + } catch (err) { + enable(); + throw err; } - - return result; }; /** diff --git a/test/lib/bypass.spec.js b/test/lib/bypass.spec.js index 5040eed..458e73b 100644 --- a/test/lib/bypass.spec.js +++ b/test/lib/bypass.spec.js @@ -64,10 +64,8 @@ describe('mock.bypass()', () => { assert.equal(fs.readFileSync('/path/to/file', 'utf8'), 'content'); assert.isFalse(fs.existsSync(__filename)); - const promise = mock.bypass(() => fs.promises.stat(__filename)); - assert.instanceOf(promise, Promise); - - promise + mock + .bypass(() => fs.promises.stat(__filename)) .then(stat => { assert.isTrue(stat.isFile()); assert.isFalse(fs.existsSync(__filename)); @@ -84,13 +82,11 @@ describe('mock.bypass()', () => { const error = new Error('oops'); - const promise = mock.bypass(() => { - assert.isTrue(fs.existsSync(__filename)); - return Promise.reject(error); - }); - assert.instanceOf(promise, Promise); - - promise + mock + .bypass(() => { + assert.isTrue(fs.existsSync(__filename)); + return Promise.reject(error); + }) .then(() => { done(new Error('should not succeed')); })