Skip to content
Open
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
3 changes: 3 additions & 0 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,9 @@ Document.prototype.$init = function() {
*/

Document.prototype.$__init = function(doc, opts) {
if (doc == null) {
throw new ObjectParameterError(doc, 'doc', 'init');
}
this.$isNew = false;
opts = opts || {};

Expand Down
32 changes: 32 additions & 0 deletions test/gh-15812.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const start = require('./common');
const mongoose = start.mongoose;
const assert = require('assert');
const ObjectParameterError = require('../lib/error/objectParameter');

describe('gh-15812', function () {

Check failure on line 8 in test/gh-15812.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix lint and please move this test into model.test.js

it('should throw ObjectParameterError when init is called with null', function () {

Check failure on line 9 in test/gh-15812.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected space before function parentheses

Check failure on line 9 in test/gh-15812.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Expected indentation of 2 spaces but found 4
const doc = new mongoose.Document({}, new mongoose.Schema({ name: String }));

Check failure on line 10 in test/gh-15812.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Expected indentation of 4 spaces but found 8
try {

Check failure on line 11 in test/gh-15812.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Expected indentation of 4 spaces but found 8
doc.init(null);

Check failure on line 12 in test/gh-15812.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Expected indentation of 6 spaces but found 12
assert.fail('Should have thrown an error');

Check failure on line 13 in test/gh-15812.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Expected indentation of 6 spaces but found 12
} catch (error) {

Check failure on line 14 in test/gh-15812.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Expected indentation of 4 spaces but found 8
assert.ok(error instanceof ObjectParameterError);

Check failure on line 15 in test/gh-15812.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Expected indentation of 6 spaces but found 12
assert.strictEqual(error.name, 'ObjectParameterError');

Check failure on line 16 in test/gh-15812.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Expected indentation of 6 spaces but found 12
assert.ok(error.message.includes('Parameter "doc" to init() must be an object'));
}
});

it('should throw ObjectParameterError when init is called with undefined', function () {
const doc = new mongoose.Document({}, new mongoose.Schema({ name: String }));
try {
doc.init(undefined);
assert.fail('Should have thrown an error');
} catch (error) {
assert.ok(error instanceof ObjectParameterError);
assert.strictEqual(error.name, 'ObjectParameterError');
assert.ok(error.message.includes('Parameter "doc" to init() must be an object'));
}
});
});