1
1
2
- import { expect } from 'chai' ;
3
2
import { 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' ;
6
4
import { Readable } from 'stream' ;
7
5
import { pipeline } from 'stream/promises' ;
8
6
import { setTimeout } from 'timers/promises' ;
@@ -17,104 +15,70 @@ async function setUpCollection(client: MongoClient) {
17
15
}
18
16
19
17
describe ( '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
-
34
18
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 ( ) ;
42
22
} )
43
23
} )
44
24
45
25
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 ( ) ;
59
36
} )
60
37
61
38
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 ( ) ;
66
42
67
- const collection = await setUpCollection ( client ) ;
43
+ const collection = await setUpCollection ( client ) ;
68
44
69
- await using readable = collection . find ( ) . stream ( ) ;
70
- }
71
- expect ( readableSpy . callCount ) . to . equal ( 1 ) ;
45
+ await using readable = collection . find ( ) . stream ( ) ;
72
46
} )
73
47
} )
74
48
} )
75
49
76
50
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 ( ) ;
85
56
} )
86
57
} )
87
58
88
59
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 ( ) ;
101
69
} )
102
70
} ) ;
103
71
104
72
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 ( ) ;
113
76
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 ) ;
115
80
116
- }
117
- expect ( readableSpy . callCount ) . to . equal ( 1 ) ;
81
+ await using downloadStream = bucket . openDownloadStreamByName ( 'foo.txt' ) ;
118
82
} )
119
83
} ) ;
120
84
} )
0 commit comments