@@ -309,15 +309,26 @@ operations.set('insertMany', async ({ entities, operation }) => {
309
309
} ) ;
310
310
311
311
operations . set ( 'iterateUntilDocumentOrError' , async ( { entities, operation } ) => {
312
- const cursorOrStream =
313
- entities . getEntity ( 'stream' , operation . object , false ) ??
314
- entities . getEntity ( 'cursor' , operation . object , false ) ;
312
+ function getChangeStream ( ) : UnifiedChangeStream | null {
313
+ try {
314
+ const changeStream = entities . getEntity ( 'stream' , operation . object ) ;
315
+ return changeStream ;
316
+ } catch ( e ) {
317
+ return null ;
318
+ }
319
+ }
315
320
316
- if ( cursorOrStream == null ) {
317
- throw new Error ( `Unable to get entity - expect stream or cursor but received ${ operation } ` ) ;
321
+ const changeStream = getChangeStream ( ) ;
322
+ if ( changeStream == null ) {
323
+ // iterateUntilDocumentOrError is used for changes streams and regular cursors.
324
+ // we have no other way to distinguish which scenario we are testing when we run an
325
+ // iterateUntilDocumentOrError operation, so we first try to get the changeStream and
326
+ // if that fails, we know we need to get a cursor
327
+ const cursor = entities . getEntity ( 'cursor' , operation . object ) ;
328
+ return await cursor . next ( ) ;
318
329
}
319
330
320
- return await cursorOrStream . next ( ) ;
331
+ return changeStream . next ( ) ;
321
332
} ) ;
322
333
323
334
operations . set ( 'listCollections' , async ( { entities, operation } ) => {
0 commit comments