Skip to content

Commit c2caa0f

Browse files
fix(model): add test to skip applying static hooks by default when static name conflicts with aggregate middleware
1 parent cd930c7 commit c2caa0f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

test/model.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const assert = require('assert');
1010
const { once } = require('events');
1111
const random = require('./util').random;
1212
const util = require('./util');
13+
const model = require('../lib/model');
1314

1415
const mongoose = start.mongoose;
1516
const Schema = mongoose.Schema;
@@ -5861,6 +5862,35 @@ describe('Model', function() {
58615862

58625863
});
58635864

5865+
it('custom statics that overwrite aggregate functions dont get hooks by default (gh-14903)', async function() {
5866+
5867+
const schema = new Schema({ name: String });
5868+
5869+
schema.statics.aggregate = function(pipeline) {
5870+
return model.aggregate.apply(this, [pipeline]);
5871+
};
5872+
5873+
let called = 0;
5874+
schema.pre('aggregate', function(next) {
5875+
++called;
5876+
next();
5877+
});
5878+
const Model = db.model('Test', schema);
5879+
5880+
await Model.create({ name: 'foo' });
5881+
5882+
const res = await Model.aggregate([
5883+
{
5884+
$match: {
5885+
name: 'foo'
5886+
}
5887+
}
5888+
]);
5889+
5890+
assert.ok(res[0].name);
5891+
assert.equal(called, 1);
5892+
});
5893+
58645894
it('error handling middleware passes saved doc (gh-7832)', async function() {
58655895
const schema = new Schema({ _id: Number });
58665896

0 commit comments

Comments
 (0)