@@ -173,7 +173,10 @@ describe('MaxTimeMS', function () {
173173 const timeIsSetOnGetMore = typeof maxAwaitTimeMS === 'number' ;
174174 return [
175175 {
176- options : { tailable, awaitData, maxAwaitTimeMS, maxTimeMS } ,
176+ // Use JSON to drop explicit undefined
177+ options : JSON . parse (
178+ JSON . stringify ( { tailable, awaitData, maxAwaitTimeMS, maxTimeMS } )
179+ ) ,
177180 outcome : {
178181 // Cannot set 'awaitData' without also setting 'tailable'
179182 isFindError : awaitDataSet && ! tailableSet ,
@@ -187,33 +190,55 @@ describe('MaxTimeMS', function () {
187190 )
188191 ) ;
189192
193+ it ( 'meta test: should setup test table correctly' , ( ) => {
194+ expect ( tests ) . to . have . lengthOf ( 81 ) ;
195+ expect ( tests . filter ( t => t . outcome . isFindError ) ) . to . have . lengthOf ( 18 ) ;
196+ expect ( tests . filter ( t => t . outcome . isGetMoreError ) ) . to . have . lengthOf ( 36 ) ;
197+ expect (
198+ tests . filter ( t => {
199+ return ! t . outcome . isFindError && ! t . outcome . isGetMoreError ;
200+ } )
201+ ) . to . have . lengthOf ( 27 ) ;
202+ } ) ;
203+
190204 for ( const { options, outcome } of tests ) {
191205 let optionsString = inspect ( options , { breakLength : Infinity } ) ;
192- optionsString = optionsString
193- . slice ( 2 , optionsString . length - 2 )
194- . split ( 'undefined' )
195- . join ( 'omit' ) ;
206+ optionsString = optionsString . slice ( 1 , optionsString . length - 1 ) . trim ( ) ;
207+ optionsString = optionsString === '' ? 'nothing set' : optionsString ;
196208
197- it ( `should create find cursor with ${ optionsString } ` , async ( ) => {
209+ const operation = async ( ) => {
198210 cursor = cappedCollection . find ( { _id : { $gt : 0 } } , { ...options , batchSize : 1 } ) ;
199-
200211 const findDocOrError : { _id : number } | Error = await cursor . next ( ) . catch ( error => error ) ;
201-
202212 const exhaustedByFind = ! ! cursor . id ?. isZero ( ) ;
203-
204213 const getMoreDocOrError : { _id : number } | Error | null = await cursor
205214 . tryNext ( )
206215 . catch ( error => error ) ;
207-
208216 expect ( events ) . to . have . length . of . at . least ( 1 ) ; // At least find must be sent
217+ return { findDocOrError, exhaustedByFind, getMoreDocOrError } ;
218+ } ;
209219
210- if ( outcome . isFindError ) {
220+ if ( outcome . isFindError ) {
221+ it ( `should error on find due to setting ${ optionsString } ` , async ( ) => {
222+ const { findDocOrError } = await operation ( ) ;
211223 expect ( findDocOrError ) . to . be . instanceOf ( MongoServerError ) ;
212- } else {
213- if ( findDocOrError instanceof Error ) {
214- throw findDocOrError ;
224+ } ) ;
225+ } else if ( outcome . isGetMoreError ) {
226+ it ( `should error on getMore due to setting ${ optionsString } ` , async ( ) => {
227+ const { exhaustedByFind, getMoreDocOrError } = await operation ( ) ;
228+ if ( exhaustedByFind ) {
229+ expect ( getMoreDocOrError ) . to . be . instanceOf ( MongoCursorExhaustedError ) ;
230+ } else {
231+ expect ( getMoreDocOrError ) . to . be . instanceOf ( MongoServerError ) ;
215232 }
216- expect ( findDocOrError ) . to . have . property ( '_id' , 1 ) ;
233+ } ) ;
234+ } else {
235+ it ( `should create find cursor with ${ optionsString } ` , async ( ) => {
236+ const { findDocOrError : findDoc , getMoreDocOrError : getMoreDoc } = await operation ( ) ;
237+
238+ expect ( findDoc ) . to . not . be . instanceOf ( Error ) ;
239+ expect ( getMoreDoc ) . to . not . be . instanceOf ( Error ) ;
240+
241+ expect ( findDoc ) . to . have . property ( '_id' , 1 ) ;
217242
218243 expect ( events [ 0 ] . command ) . to . be . an ( 'object' ) . that . has . a . property ( 'find' ) ;
219244 const findCommand = events [ 0 ] . command ;
@@ -223,17 +248,8 @@ describe('MaxTimeMS', function () {
223248 } else {
224249 expect ( findCommand ) . to . not . have . property ( 'maxTimeMS' ) ;
225250 }
226- }
227-
228- if ( outcome . isGetMoreError ) {
229- expect ( getMoreDocOrError ) . to . be . instanceOf ( MongoServerError ) ;
230- } else if ( exhaustedByFind ) {
231- expect ( getMoreDocOrError ) . to . be . instanceOf ( MongoCursorExhaustedError ) ;
232- } else {
233- if ( getMoreDocOrError instanceof Error ) {
234- throw getMoreDocOrError ;
235- }
236- expect ( getMoreDocOrError ) . to . be . null ;
251+
252+ expect ( getMoreDoc ) . to . be . null ;
237253
238254 expect ( events [ 1 ] . command ) . to . be . an ( 'object' ) . that . has . a . property ( 'getMore' ) ;
239255 const getMoreCommand = events [ 1 ] . command ;
@@ -243,10 +259,8 @@ describe('MaxTimeMS', function () {
243259 } else {
244260 expect ( getMoreCommand ) . to . not . have . property ( 'maxTimeMS' ) ;
245261 }
246- }
247-
248- await cursor . close ( ) ;
249- } ) ;
262+ } ) ;
263+ }
250264 }
251265 } ) ;
252266} ) ;
0 commit comments