Skip to content

Commit 7f225e8

Browse files
committed
test(document.test): test hooks for "validateSync" to skip async functions and promises
1 parent eeefdd2 commit 7f225e8

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

test/document.test.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12262,6 +12262,74 @@ describe('document', function() {
1226212262
const test2 = {};
1226312263
assert.strictEqual(test2.constructor.polluted, undefined);
1226412264
assert.strictEqual(Object.polluted, undefined);
12265+
12266+
});
12267+
12268+
it('validateSync executes error hook and ignores async function (gh-4885)', function(done) {
12269+
let asyncCalled = false;
12270+
let errorHookCalled = false;
12271+
let normalHookCalled = false;
12272+
12273+
const schema = new Schema({
12274+
prop: Boolean
12275+
});
12276+
12277+
schema.post('validate', async function() { asyncCalled = true; });
12278+
schema.post('validate', () => { normalHookCalled = true; throw new Error('test error for error hook'); });
12279+
schema.post('validate', function(err, doc, next) {
12280+
errorHookCalled = true;
12281+
next();
12282+
});
12283+
12284+
const model = db.model('test', schema);
12285+
12286+
const doc = new model({ prop: true });
12287+
doc.validateSync();
12288+
12289+
// schema.path('prop').doValidateSync(true);
12290+
12291+
assert.strictEqual(normalHookCalled, true);
12292+
assert.strictEqual(asyncCalled, false);
12293+
assert.strictEqual(errorHookCalled, true);
12294+
done();
12295+
});
12296+
12297+
it('validateSync executes error hook and ignores returned Promises (gh-4885)', function(done) {
12298+
let asyncCalled = false;
12299+
let asyncCompleted = false;
12300+
let errorHookCalled = false;
12301+
let normalHookCalled = false;
12302+
12303+
const schema = new Schema({
12304+
prop: Boolean
12305+
});
12306+
12307+
schema.post('validate', function() {
12308+
asyncCalled = true;
12309+
return new Promise((res) => setTimeout(() => {
12310+
asyncCompleted = true;
12311+
return res();
12312+
}, 1000));
12313+
});
12314+
schema.post('validate', () => { normalHookCalled = true; throw new Error('test error for error hook'); });
12315+
schema.post('validate', function(err, doc, next) {
12316+
errorHookCalled = true;
12317+
assert.ok(err);
12318+
next();
12319+
});
12320+
12321+
// schema.path('prop').doValidateSync(true);
12322+
12323+
const model = db.model('test', schema);
12324+
12325+
const doc = new model({ prop: true });
12326+
doc.validateSync();
12327+
12328+
assert.strictEqual(normalHookCalled, true);
12329+
assert.strictEqual(asyncCalled, true);
12330+
assert.strictEqual(asyncCompleted, false);
12331+
assert.strictEqual(errorHookCalled, true);
12332+
done();
1226512333
});
1226612334
});
1226712335

@@ -12273,4 +12341,3 @@ describe('Check if instance function that is supplied in schema option is availa
1227312341
assert.equal(TestDocument.instanceFn(), 'Returned from DocumentInstanceFn');
1227412342
});
1227512343
});
12276-

0 commit comments

Comments
 (0)