Skip to content

Commit 025d952

Browse files
add sinon assertions
1 parent cc424d5 commit 025d952

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

test/explicit-resource-management/main.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11

22
import { describe, it } from 'mocha';
3-
import { GridFSBucket, MongoClient } from 'mongodb/lib/beta';
3+
import { AbstractCursor, ChangeStream, ClientSession, GridFSBucket, MongoClient } from 'mongodb/lib/beta';
44
import { Readable } from 'stream';
55
import { pipeline } from 'stream/promises';
66
import { expect } from 'chai';
77
import { setTimeout } from 'timers/promises';
88
import { createReadStream } from 'fs';
99
import { join } from 'path';
1010

11+
import * as sinon from 'sinon';
12+
1113
async function setUpCollection(client: MongoClient) {
1214
const collection = client.db('foo').collection<{ name: string }>('bar');
1315
const documents: Array<{ name: string }> = Array.from({ length: 5 }).map(i => ({
@@ -18,6 +20,19 @@ async function setUpCollection(client: MongoClient) {
1820
}
1921

2022
describe('explicit resource management feature integration tests', function () {
23+
const clientDisposeSpy = sinon.spy(MongoClient.prototype, Symbol.asyncDispose);
24+
const sessionDisposeSpy = sinon.spy(ClientSession.prototype, Symbol.asyncDispose);
25+
const changeStreamDisposeSpy = sinon.spy(ChangeStream.prototype, Symbol.asyncDispose);
26+
const cursorDisposeSpy = sinon.spy(AbstractCursor.prototype, Symbol.asyncDispose);
27+
const readableDisposeSpy = sinon.spy(Readable.prototype, Symbol.asyncDispose);
28+
29+
afterEach(function(){
30+
clientDisposeSpy.resetHistory();
31+
sessionDisposeSpy.resetHistory();
32+
changeStreamDisposeSpy.resetHistory();
33+
cursorDisposeSpy.resetHistory();
34+
readableDisposeSpy.resetHistory();
35+
})
2136
describe('MongoClient', function () {
2237
it('does not crash or error when used with await-using syntax', async function () {
2338
await using client = new MongoClient(process.env.MONGODB_URI!);
@@ -33,6 +48,7 @@ describe('explicit resource management feature integration tests', function () {
3348
})().catch(e => e);
3449

3550
expect(error).to.match(/error thrown/);
51+
expect(clientDisposeSpy.called).to.be.true;
3652
});
3753

3854
it('works if client is explicitly closed', async function () {
@@ -73,6 +89,7 @@ describe('explicit resource management feature integration tests', function () {
7389
})().catch(e => e);
7490

7591
expect(error).to.match(/error thrown/);
92+
expect(cursorDisposeSpy.called).to.be.true;
7693
});
7794

7895
it('works if cursor is explicitly closed', async function () {
@@ -116,6 +133,7 @@ describe('explicit resource management feature integration tests', function () {
116133
})().catch(e => e);
117134

118135
expect(error).to.match(/error thrown/);
136+
expect(readableDisposeSpy.called).to.be.true;
119137
});
120138

121139
it('works if stream is explicitly closed', async function () {
@@ -157,6 +175,7 @@ describe('explicit resource management feature integration tests', function () {
157175
})().catch(e => e);
158176

159177
expect(error).to.match(/error thrown/);
178+
expect(sessionDisposeSpy.called).to.be.true;
160179
});
161180

162181
it('works if session is explicitly closed', async function () {
@@ -202,6 +221,7 @@ describe('explicit resource management feature integration tests', function () {
202221
})().catch(e => e);
203222

204223
expect(error).to.match(/error thrown/);
224+
expect(changeStreamDisposeSpy.called).to.be.true;
205225
});
206226

207227
it('works if change stream is explicitly closed', async function () {
@@ -250,6 +270,7 @@ describe('explicit resource management feature integration tests', function () {
250270
})().catch(e => e);
251271

252272
expect(error).to.match(/error thrown/);
273+
expect(readableDisposeSpy.called).to.be.true;
253274
});
254275

255276
it('works if stream is explicitly closed', async function () {

0 commit comments

Comments
 (0)