Skip to content

Commit

Permalink
test(document.test): test hooks for "validateSync" to skip async func…
Browse files Browse the repository at this point in the history
…tions and promises
  • Loading branch information
hasezoey committed Jul 13, 2023
1 parent eeefdd2 commit 7f225e8
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12262,6 +12262,74 @@ describe('document', function() {
const test2 = {};
assert.strictEqual(test2.constructor.polluted, undefined);
assert.strictEqual(Object.polluted, undefined);

});

it('validateSync executes error hook and ignores async function (gh-4885)', function(done) {
let asyncCalled = false;
let errorHookCalled = false;
let normalHookCalled = false;

const schema = new Schema({
prop: Boolean
});

schema.post('validate', async function() { asyncCalled = true; });
schema.post('validate', () => { normalHookCalled = true; throw new Error('test error for error hook'); });
schema.post('validate', function(err, doc, next) {
errorHookCalled = true;
next();
});

const model = db.model('test', schema);

const doc = new model({ prop: true });
doc.validateSync();

// schema.path('prop').doValidateSync(true);

assert.strictEqual(normalHookCalled, true);
assert.strictEqual(asyncCalled, false);
assert.strictEqual(errorHookCalled, true);
done();
});

it('validateSync executes error hook and ignores returned Promises (gh-4885)', function(done) {
let asyncCalled = false;
let asyncCompleted = false;
let errorHookCalled = false;
let normalHookCalled = false;

const schema = new Schema({
prop: Boolean
});

schema.post('validate', function() {
asyncCalled = true;
return new Promise((res) => setTimeout(() => {
asyncCompleted = true;
return res();
}, 1000));
});
schema.post('validate', () => { normalHookCalled = true; throw new Error('test error for error hook'); });
schema.post('validate', function(err, doc, next) {
errorHookCalled = true;
assert.ok(err);
next();
});

// schema.path('prop').doValidateSync(true);

const model = db.model('test', schema);

const doc = new model({ prop: true });
doc.validateSync();

assert.strictEqual(normalHookCalled, true);
assert.strictEqual(asyncCalled, true);
assert.strictEqual(asyncCompleted, false);
assert.strictEqual(errorHookCalled, true);
done();
});
});

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

0 comments on commit 7f225e8

Please sign in to comment.