Skip to content

Commit 969fd1d

Browse files
committed
test: improve tests for overwriteMiddlewareResult() and skipMiddlewareFunction()
1 parent 01c1d87 commit 969fd1d

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

test/query.middleware.test.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -657,19 +657,22 @@ describe('query middleware', function() {
657657

658658
it('allows skipping the wrapped function with `skipMiddlewareFunction()` (gh-11426)', async function() {
659659
const schema = Schema({ name: String });
660+
const now = Date.now();
660661

661-
schema.pre('updateOne', function(next) {
662-
next(mongoose.skipMiddlewareFunction({ answer: 42 }));
662+
schema.pre('find', function(next) {
663+
next(mongoose.skipMiddlewareFunction([{ name: 'from cache' }]));
664+
});
665+
schema.post('find', function(res) {
666+
res.forEach(doc => {
667+
doc.loadedAt = now;
668+
});
663669
});
664670
const Test = db.model('Test', schema);
665671

666-
const { _id } = await Test.create({ name: 'test' });
667-
const res = await Test.updateOne({ _id }, { name: 'changed' });
668-
assert.equal(res.answer, 42);
669-
assert.strictEqual(res.modifiedCount, undefined);
670-
671-
const doc = await Test.findById(_id);
672-
assert.equal(doc.name, 'test');
672+
const res = await Test.find();
673+
assert.equal(res.length, 1);
674+
assert.strictEqual(res[0].name, 'from cache');
675+
assert.strictEqual(res[0].loadedAt, now);
673676
});
674677

675678
it('allows overwriting result with `overwriteMiddlewareResult()` (gh-11426)', async function() {
@@ -678,12 +681,17 @@ describe('query middleware', function() {
678681
schema.post('updateOne', function() {
679682
return mongoose.overwriteMiddlewareResult({ answer: 42 });
680683
});
684+
schema.post('updateOne', function(res) {
685+
assert.strictEqual(res.answer, 42);
686+
res.secondMiddlewareRan = true;
687+
});
681688
const Test = db.model('Test', schema);
682689

683690
const { _id } = await Test.create({ name: 'test' });
684691
const res = await Test.updateOne({ _id }, { name: 'changed' });
685692
assert.equal(res.answer, 42);
686693
assert.strictEqual(res.modifiedCount, undefined);
694+
assert.strictEqual(res.secondMiddlewareRan, true);
687695

688696
const doc = await Test.findById(_id);
689697
assert.equal(doc.name, 'changed');

0 commit comments

Comments
 (0)