11
2- import { expect } from 'chai' ;
32import { describe , it } from 'mocha' ;
4- import { AbstractCursor , ChangeStream , ClientSession , GridFSBucket , MongoClient } from 'mongodb/lib/beta' ;
5- import * as sinon from 'sinon' ;
3+ import { GridFSBucket , MongoClient } from 'mongodb/lib/beta' ;
64import { Readable } from 'stream' ;
75import { pipeline } from 'stream/promises' ;
86import { setTimeout } from 'timers/promises' ;
@@ -17,104 +15,70 @@ async function setUpCollection(client: MongoClient) {
1715}
1816
1917describe ( 'explicit resource management smoke tests' , function ( ) {
20- const clientSpy = sinon . spy ( MongoClient . prototype , Symbol . asyncDispose ) ;
21- const cursorSpy = sinon . spy ( AbstractCursor . prototype , Symbol . asyncDispose ) ;
22- const endSessionSpy = sinon . spy ( ClientSession . prototype , Symbol . asyncDispose ) ;
23- const changeStreamSpy = sinon . spy ( ChangeStream . prototype , Symbol . asyncDispose ) ;
24- const readableSpy = sinon . spy ( Readable . prototype , Symbol . asyncDispose ) ;
25-
26- afterEach ( function ( ) {
27- clientSpy . resetHistory ( ) ;
28- cursorSpy . resetHistory ( ) ;
29- endSessionSpy . resetHistory ( ) ;
30- changeStreamSpy . resetHistory ( ) ;
31- readableSpy . resetHistory ( ) ;
32- } ) ;
33-
3418 describe ( 'MongoClient' , function ( ) {
35- it ( 'can be used with await-using syntax' , async function ( ) {
36- {
37- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
38- await client . connect ( ) ;
39- }
40- expect ( clientSpy . called ) . to . be . true ;
41- expect ( clientSpy . callCount ) . to . equal ( 1 ) ;
19+ it ( 'does not crash or error when used with await-using syntax' , async function ( ) {
20+ await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
21+ await client . connect ( ) ;
4222 } )
4323 } )
4424
4525 describe ( 'Cursors' , function ( ) {
46- it ( 'can be used with await-using syntax' , async function ( ) {
47- {
48- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
49- await client . connect ( ) ;
50-
51- const collection = await setUpCollection ( client ) ;
52-
53- await using cursor = collection . find ( ) ;
54- await cursor . next ( ) ;
55- await cursor . next ( ) ;
56- await cursor . next ( ) ;
57- }
58- expect ( cursorSpy . callCount ) . to . equal ( 1 ) ;
26+ it ( 'does not crash or error when used with await-using syntax' , async function ( ) {
27+ await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
28+ await client . connect ( ) ;
29+
30+ const collection = await setUpCollection ( client ) ;
31+
32+ await using cursor = collection . find ( ) ;
33+ await cursor . next ( ) ;
34+ await cursor . next ( ) ;
35+ await cursor . next ( ) ;
5936 } )
6037
6138 describe ( 'cursor streams' , function ( ) {
62- it ( 'can be used with await-using syntax' , async function ( ) {
63- {
64- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
65- await client . connect ( ) ;
39+ it ( 'does not crash or error when used with await-using syntax' , async function ( ) {
40+ await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
41+ await client . connect ( ) ;
6642
67- const collection = await setUpCollection ( client ) ;
43+ const collection = await setUpCollection ( client ) ;
6844
69- await using readable = collection . find ( ) . stream ( ) ;
70- }
71- expect ( readableSpy . callCount ) . to . equal ( 1 ) ;
45+ await using readable = collection . find ( ) . stream ( ) ;
7246 } )
7347 } )
7448 } )
7549
7650 describe ( 'Sessions' , function ( ) {
77- it ( 'can be used with await-using syntax' , async function ( ) {
78- {
79- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
80- await client . connect ( ) ;
81-
82- await using session = client . startSession ( ) ;
83- }
84- expect ( endSessionSpy . callCount ) . to . equal ( 1 ) ;
51+ it ( 'does not crash or error when used with await-using syntax' , async function ( ) {
52+ await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
53+ await client . connect ( ) ;
54+
55+ await using session = client . startSession ( ) ;
8556 } )
8657 } )
8758
8859 describe ( 'ChangeStreams' , function ( ) {
89- it ( 'can be used with await-using syntax' , async function ( ) {
90- {
91- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
92- await client . connect ( ) ;
93-
94- const collection = await setUpCollection ( client ) ;
95- await using cs = collection . watch ( ) ;
96-
97- setTimeout ( 1000 ) . then ( ( ) => collection . insertOne ( { name : 'bailey' } ) ) ;
98- await cs . next ( ) ;
99- }
100- expect ( changeStreamSpy . callCount ) . to . equal ( 1 ) ;
60+ it ( 'does not crash or error when used with await-using syntax' , async function ( ) {
61+ await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
62+ await client . connect ( ) ;
63+
64+ const collection = await setUpCollection ( client ) ;
65+ await using cs = collection . watch ( ) ;
66+
67+ setTimeout ( 1000 ) . then ( ( ) => collection . insertOne ( { name : 'bailey' } ) ) ;
68+ await cs . next ( ) ;
10169 } )
10270 } ) ;
10371
10472 describe ( 'GridFSDownloadStream' , function ( ) {
105- it ( 'can be used with await-using syntax' , async function ( ) {
106- {
107- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
108- await client . connect ( ) ;
109-
110- const bucket = new GridFSBucket ( client . db ( 'foo' ) ) ;
111- const uploadStream = bucket . openUploadStream ( 'foo.txt' )
112- await pipeline ( Readable . from ( "AAAAAAA" . split ( '' ) ) , uploadStream ) ;
73+ it ( 'does not crash or error when used with await-using syntax' , async function ( ) {
74+ await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
75+ await client . connect ( ) ;
11376
114- await using downloadStream = bucket . openDownloadStreamByName ( 'foo.txt' ) ;
77+ const bucket = new GridFSBucket ( client . db ( 'foo' ) ) ;
78+ const uploadStream = bucket . openUploadStream ( 'foo.txt' )
79+ await pipeline ( Readable . from ( "AAAAAAA" . split ( '' ) ) , uploadStream ) ;
11580
116- }
117- expect ( readableSpy . callCount ) . to . equal ( 1 ) ;
81+ await using downloadStream = bucket . openDownloadStreamByName ( 'foo.txt' ) ;
11882 } )
11983 } ) ;
12084} )
0 commit comments