summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/shell_cmd_assertions.js
diff options
context:
space:
mode:
Diffstat (limited to 'jstests/noPassthrough/shell_cmd_assertions.js')
-rw-r--r--jstests/noPassthrough/shell_cmd_assertions.js38
1 files changed, 38 insertions, 0 deletions
diff --git a/jstests/noPassthrough/shell_cmd_assertions.js b/jstests/noPassthrough/shell_cmd_assertions.js
index d0a5dbaeaff..669b442a37d 100644
--- a/jstests/noPassthrough/shell_cmd_assertions.js
+++ b/jstests/noPassthrough/shell_cmd_assertions.js
@@ -1,3 +1,6 @@
+/**
+ * Tests for the command assertion functions in mongo/shell/assert.js.
+ */
(function() {
"use strict";
const conn = MongoRunner.runMongod();
@@ -19,6 +22,40 @@
assert.throws(() => assert.commandFailedWithCode(res, 0));
});
+ function _assertMsgFunctionExecution(
+ assertFunc, assertParameter, {expectException: expectException = false} = {}) {
+ var msgFunctionCalled = false;
+ var expectedAssert = assert.doesNotThrow;
+
+ if (expectException) {
+ expectedAssert = assert.throws;
+ }
+
+ expectedAssert(() => {
+ assertFunc(assertParameter, () => {
+ msgFunctionCalled = true;
+ });
+ });
+
+ assert.eq(
+ expectException, msgFunctionCalled, "msg function execution should match assertion");
+ }
+
+ tests.push(function msgFunctionOnlyCalledOnFailure() {
+ const res = db.runCommand({"ping": 1});
+
+ _assertMsgFunctionExecution(assert.commandWorked, res, {expectException: false});
+ _assertMsgFunctionExecution(
+ assert.commandWorkedIgnoringWriteErrors, res, {expectException: false});
+ _assertMsgFunctionExecution(assert.commandFailed, res, {expectException: true});
+
+ var msgFunctionCalled = false;
+ assert.throws(() => assert.commandFailedWithCode(res, 0, () => {
+ msgFunctionCalled = true;
+ }));
+ assert.eq(true, msgFunctionCalled, "msg function execution should match assertion");
+ });
+
tests.push(function rawCommandErr() {
const res = db.runCommand({"IHopeNobodyEverMakesThisACommand": 1});
assert.throws(() => assert.commandWorked(res));
@@ -252,5 +289,6 @@
test();
});
+ /* cleanup */
MongoRunner.stopMongod(conn);
})();