File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ const assert = require('assert');
1010const { once } = require ( 'events' ) ;
1111const random = require ( './util' ) . random ;
1212const util = require ( './util' ) ;
13+ const model = require ( '../lib/model' ) ;
1314
1415const mongoose = start . mongoose ;
1516const 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
You can’t perform that action at this time.
0 commit comments