@@ -372,16 +372,31 @@ describe('Change Streams', function () {
372372
373373 describe ( 'cache the change stream resume token' , ( ) => {
374374 describe ( 'using iterator form' , ( ) => {
375- it ( '#next' , {
376- metadata : { requires : { topology : 'replicaset' } } ,
375+ context ( '#next' , ( ) => {
376+ it ( 'caches the resume token on change' , {
377+ metadata : { requires : { topology : 'replicaset' } } ,
377378
378- async test ( ) {
379- await initIteratorMode ( changeStream ) ;
380- await collection . insertOne ( { a : 1 } ) ;
379+ async test ( ) {
380+ await initIteratorMode ( changeStream ) ;
381+ await collection . insertOne ( { a : 1 } ) ;
381382
382- const change = await changeStream . next ( ) ;
383- expect ( change ) . to . have . property ( '_id' ) . that . deep . equals ( changeStream . resumeToken ) ;
384- }
383+ const change = await changeStream . next ( ) ;
384+ expect ( change ) . to . have . property ( '_id' ) . that . deep . equals ( changeStream . resumeToken ) ;
385+ }
386+ } ) ;
387+
388+ it ( 'caches the resume token correctly when preceded by #hasNext' , {
389+ metadata : { requires : { topology : 'replicaset' } } ,
390+ async test ( ) {
391+ await initIteratorMode ( changeStream ) ;
392+ await collection . insertOne ( { a : 1 } ) ;
393+
394+ await changeStream . hasNext ( ) ;
395+
396+ const change = await changeStream . next ( ) ;
397+ expect ( change ) . to . have . property ( '_id' ) . that . deep . equals ( changeStream . resumeToken ) ;
398+ }
399+ } ) ;
385400 } ) ;
386401
387402 it ( '#tryNext' , {
@@ -396,7 +411,7 @@ describe('Change Streams', function () {
396411 }
397412 } ) ;
398413
399- describe ( '#hasNext' , ( ) => {
414+ context ( '#hasNext' , ( ) => {
400415 it ( 'does not cache the resume token' , {
401416 metadata : { requires : { topology : 'replicaset' } } ,
402417 async test ( ) {
@@ -408,14 +423,7 @@ describe('Change Streams', function () {
408423 const hasNext = await changeStream . hasNext ( ) ;
409424 expect ( hasNext ) . to . be . true ;
410425
411- // Calling .hasNext() does not allow the ChangeStream to update the token,
412- // even when changes are present.
413426 expect ( changeStream . resumeToken ) . to . equal ( resumeToken ) ;
414-
415- // Consuming the change causes the ChangeStream to cache the resume token.
416- const change = await changeStream . next ( ) ;
417- expect ( change ) . to . have . property ( '_id' ) . that . deep . equals ( changeStream . resumeToken ) ;
418- expect ( resumeToken ) . to . not . equal ( changeStream . resumeToken ) ;
419427 }
420428 } ) ;
421429 } ) ;
0 commit comments