Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion snippets/mongocompat/mongotypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,9 @@ tojsonObject = function(x, indent, nolint, depth) {
}
var lineEnding = nolint ? " " : "\n";
var tabSpace = nolint ? "" : "\t";
assert.eq((typeof x), "object", "tojsonObject needs object, not [" + (typeof x) + "]");
if (typeof x !== "object") {
throw new TypeError(`tojsonObject needs object, not [${typeof x}]`);
}

if (!indent)
indent = "";
Expand Down
16 changes: 16 additions & 0 deletions snippets/mongocompat/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,19 @@ dbRefForSetters.$db = 'newDb';
assert.strictEqual(dbRefForSetters.$db, 'newDb');
assert.strictEqual(dbRefForSetters.db, 'newDb');
assert.strictEqual(dbRefForSetters.toString(), 'DBRef("newColl", ObjectId("507f1f77bcf86cd799439011"), "newDb")');

try {
tojsonObject("not an object");
assert.fail('Should throw TypeError for string');
} catch (e) {
assert(e instanceof TypeError);
assert(e.message.includes('tojsonObject needs object, not [string]'));
}
try {
tojsonObject(true);
assert.fail('Should throw TypeError for boolean');
} catch (e) {
assert(e.message.includes('tojsonObject needs object, not [boolean]'));
}
assert.strictEqual(typeof tojsonObject({ key: "value" }), 'string');
assert.strictEqual(typeof tojsonObject([1, 2, 3]), 'string');