Skip to content

Commit

Permalink
test(executeOperation): test throwing of errors in executeOperation
Browse files Browse the repository at this point in the history
  • Loading branch information
daprahamian committed Feb 27, 2018
1 parent 8a26081 commit b7bc5c3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
1 change: 0 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ const executeOperation = (topology, operation, args, options) => {
return operation.apply(null, args);
} catch (e) {
handler(e);
throw e;
}
});
};
Expand Down
62 changes: 62 additions & 0 deletions test/unit/execute_operation_tests.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
});

0 comments on commit b7bc5c3

Please sign in to comment.