Skip to content

Commit ff51028

Browse files
STREAMS-1972: Replace assert (#20)
1 parent 0d95a07 commit ff51028

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

snippets/mongocompat/mongotypes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,9 @@ tojsonObject = function(x, indent, nolint, depth) {
757757
}
758758
var lineEnding = nolint ? " " : "\n";
759759
var tabSpace = nolint ? "" : "\t";
760-
assert.eq((typeof x), "object", "tojsonObject needs object, not [" + (typeof x) + "]");
760+
if (typeof x !== "object") {
761+
throw new TypeError(`tojsonObject needs object, not [${typeof x}]`);
762+
}
761763

762764
if (!indent)
763765
indent = "";

snippets/mongocompat/test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,19 @@ dbRefForSetters.$db = 'newDb';
9797
assert.strictEqual(dbRefForSetters.$db, 'newDb');
9898
assert.strictEqual(dbRefForSetters.db, 'newDb');
9999
assert.strictEqual(dbRefForSetters.toString(), 'DBRef("newColl", ObjectId("507f1f77bcf86cd799439011"), "newDb")');
100+
101+
try {
102+
tojsonObject("not an object");
103+
assert.fail('Should throw TypeError for string');
104+
} catch (e) {
105+
assert(e instanceof TypeError);
106+
assert(e.message.includes('tojsonObject needs object, not [string]'));
107+
}
108+
try {
109+
tojsonObject(true);
110+
assert.fail('Should throw TypeError for boolean');
111+
} catch (e) {
112+
assert(e.message.includes('tojsonObject needs object, not [boolean]'));
113+
}
114+
assert.strictEqual(typeof tojsonObject({ key: "value" }), 'string');
115+
assert.strictEqual(typeof tojsonObject([1, 2, 3]), 'string');

0 commit comments

Comments
 (0)