Skip to content

Commit c647a05

Browse files
fix(model): skip applying static hooks by default if static name conflicts with model,document middleware
1 parent 8655c6c commit c647a05

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

test/model.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5891,6 +5891,54 @@ describe('Model', function() {
58915891
assert.equal(called, 1);
58925892
});
58935893

5894+
it('custom statics that overwrite model functions dont get hooks by default', async function() {
5895+
5896+
const schema = new Schema({ name: String });
5897+
5898+
schema.statics.insertMany = function(docs) {
5899+
return model.insertMany.apply(this, [docs]);
5900+
};
5901+
5902+
let called = 0;
5903+
schema.pre('insertMany', function(next) {
5904+
++called;
5905+
next();
5906+
});
5907+
const Model = db.model('Test', schema);
5908+
5909+
const res = await Model.insertMany([
5910+
{ name: 'foo' },
5911+
{ name: 'boo' }
5912+
]);
5913+
5914+
assert.ok(res[0].name);
5915+
assert.ok(res[1].name);
5916+
assert.equal(called, 1);
5917+
});
5918+
5919+
it('custom statics that overwrite document functions dont get hooks by default', async function() {
5920+
5921+
const schema = new Schema({ name: String });
5922+
5923+
schema.statics.save = async function() {
5924+
return 'foo';
5925+
};
5926+
5927+
let called = 0;
5928+
schema.pre('save', function(next) {
5929+
++called;
5930+
next();
5931+
});
5932+
5933+
const Model = db.model('Test', schema);
5934+
5935+
const doc = await Model.save();
5936+
5937+
assert.ok(doc);
5938+
assert.equal(doc, 'foo');
5939+
assert.equal(called, 0);
5940+
});
5941+
58945942
it('error handling middleware passes saved doc (gh-7832)', async function() {
58955943
const schema = new Schema({ _id: Number });
58965944

0 commit comments

Comments
 (0)