From b7bc5c3c25fb2bd21616c8fc8ae0b53434eafff5 Mon Sep 17 00:00:00 2001 From: Dan Aprahamian Date: Fri, 23 Feb 2018 17:24:35 -0500 Subject: [PATCH] test(executeOperation): test throwing of errors in executeOperation --- lib/utils.js | 1 - test/unit/execute_operation_tests.js | 62 ++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 test/unit/execute_operation_tests.js diff --git a/lib/utils.js b/lib/utils.js index b93ae643bb5..c89725dec93 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -436,7 +436,6 @@ const executeOperation = (topology, operation, args, options) => { return operation.apply(null, args); } catch (e) { handler(e); - throw e; } }); }; diff --git a/test/unit/execute_operation_tests.js b/test/unit/execute_operation_tests.js new file mode 100644 index 00000000000..0f3cf9e7277 --- /dev/null +++ b/test/unit/execute_operation_tests.js @@ -0,0 +1,62 @@ +'use strict'; + +const expect = require('chai').expect; +const executeOperation = require('../../lib/utils').executeOperation; + +describe('executeOperation', function() { + it('should call callback with errors on throw errors, and rethrow error', function() { + const expectedError = new Error('THIS IS AN ERROR'); + let callbackError, caughtError; + + const topology = { + logicalSessionTimeoutMinutes: null, + s: { + promiseLibrary: Promise + } + }; + const operation = () => { + throw expectedError; + }; + + const callback = err => (callbackError = err); + const options = { skipSessions: true }; + + try { + executeOperation(topology, operation, [{}, callback], options); + } catch (e) { + caughtError = e; + } + + expect(callbackError).to.equal(expectedError); + expect(caughtError).to.equal(expectedError); + }); + + it('should reject promise with errors on throw errors, and rethrow error', function(done) { + const expectedError = new Error('THIS IS AN ERROR'); + let callbackError; + + const topology = { + logicalSessionTimeoutMinutes: null, + s: { + promiseLibrary: Promise + } + }; + const operation = () => { + throw expectedError; + }; + + const callback = err => (callbackError = err); + const options = { skipSessions: true }; + + executeOperation(topology, operation, [{}, null], options).then(null, callback); + + setTimeout(() => { + try { + expect(callbackError).to.equal(expectedError); + done(); + } catch (e) { + done(e); + } + }); + }); +});