@@ -636,7 +636,6 @@ describe('query middleware', function() {
636636 } ) ;
637637 const Model = db . model ( 'Test' , schema ) ;
638638
639-
640639 await Model . find ( ) ;
641640 assert . equal ( called , 1 ) ;
642641
@@ -655,4 +654,38 @@ describe('query middleware', function() {
655654 await Model . aggregate ( [ { $match : { name : 'test' } } ] ) ;
656655 assert . equal ( called , 3 ) ;
657656 } ) ;
657+
658+ it ( 'allows skipping the wrapped function with `skipMiddlewareFunction()` (gh-11426)' , async function ( ) {
659+ const schema = Schema ( { name : String } ) ;
660+
661+ schema . pre ( 'updateOne' , function ( next ) {
662+ next ( mongoose . skipMiddlewareFunction ( { answer : 42 } ) ) ;
663+ } ) ;
664+ const Test = db . model ( 'Test' , schema ) ;
665+
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' ) ;
673+ } ) ;
674+
675+ it ( 'allows overwriting result with `overwriteMiddlewareResult()` (gh-11426)' , async function ( ) {
676+ const schema = Schema ( { name : String } ) ;
677+
678+ schema . post ( 'updateOne' , function ( ) {
679+ return mongoose . overwriteMiddlewareResult ( { answer : 42 } ) ;
680+ } ) ;
681+ const Test = db . model ( 'Test' , schema ) ;
682+
683+ const { _id } = await Test . create ( { name : 'test' } ) ;
684+ const res = await Test . updateOne ( { _id } , { name : 'changed' } ) ;
685+ assert . equal ( res . answer , 42 ) ;
686+ assert . strictEqual ( res . modifiedCount , undefined ) ;
687+
688+ const doc = await Test . findById ( _id ) ;
689+ assert . equal ( doc . name , 'changed' ) ;
690+ } ) ;
658691} ) ;
0 commit comments