Skip to content

Commit 63208b6

Browse files
committed
test: refactor finallys out
1 parent 9d938e4 commit 63208b6

File tree

1 file changed

+38
-66
lines changed

1 file changed

+38
-66
lines changed

test/integration/auth/auth.prose.test.ts

Lines changed: 38 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ describe('Authentication Spec Prose Tests', function () {
4141
};
4242
const users = Object.keys(userMap).map(name => userMap[name]);
4343
let utilClient: MongoClient;
44+
let client: MongoClient;
4445

4546
/**
4647
* Step 1
@@ -66,6 +67,8 @@ describe('Authentication Spec Prose Tests', function () {
6667
afterEach(async function () {
6768
await Promise.all(users.map(user => utilClient.db('admin').removeUser(user.username)));
6869
await utilClient?.close();
70+
await client?.close();
71+
sinon.restore();
6972
});
7073

7174
/**
@@ -90,13 +93,9 @@ describe('Authentication Spec Prose Tests', function () {
9093
authSource: 'admin'
9194
};
9295

93-
const client = this.configuration.newClient({}, options);
94-
try {
95-
const stats = await client.db('test').stats();
96-
expect(stats).to.exist;
97-
} finally {
98-
await client.close();
99-
}
96+
client = this.configuration.newClient({}, options);
97+
const stats = await client.db('test').stats();
98+
expect(stats).to.exist;
10099
}
101100
);
102101

@@ -113,13 +112,9 @@ describe('Authentication Spec Prose Tests', function () {
113112
password
114113
)}authMechanism=${mechanism}`;
115114

116-
const client = this.configuration.newClient(url);
117-
try {
118-
const stats = await client.db('test').stats();
119-
expect(stats).to.exist;
120-
} finally {
121-
await client.close();
122-
}
115+
client = this.configuration.newClient(url);
116+
const stats = await client.db('test').stats();
117+
expect(stats).to.exist;
123118
}
124119
);
125120
}
@@ -136,13 +131,9 @@ describe('Authentication Spec Prose Tests', function () {
136131
authSource: 'admin'
137132
};
138133

139-
const client = this.configuration.newClient({}, options);
140-
try {
141-
const stats = await client.db('test').stats();
142-
expect(stats).to.exist;
143-
} finally {
144-
await client.close();
145-
}
134+
client = this.configuration.newClient({}, options);
135+
const stats = await client.db('test').stats();
136+
expect(stats).to.exist;
146137
}
147138
);
148139

@@ -154,13 +145,9 @@ describe('Authentication Spec Prose Tests', function () {
154145
const password = encodeURIComponent(user.password);
155146
const url = makeConnectionString(this.configuration, username, password);
156147

157-
const client = this.configuration.newClient(url);
158-
try {
159-
const stats = await client.db('test').stats();
160-
expect(stats).to.exist;
161-
} finally {
162-
await client.close();
163-
}
148+
client = this.configuration.newClient(url);
149+
const stats = await client.db('test').stats();
150+
expect(stats).to.exist;
164151
}
165152
);
166153
}
@@ -184,16 +171,11 @@ describe('Authentication Spec Prose Tests', function () {
184171
authSource: this.configuration.db
185172
};
186173

174+
client = this.configuration.newClient({}, options);
187175
const spy = sinon.spy(ScramSHA256.prototype, 'auth');
188-
const client = this.configuration.newClient({}, options);
189-
try {
190-
const stats = await client.db('test').stats();
191-
expect(stats).to.exist;
192-
expect(spy.called).to.equal(true);
193-
} finally {
194-
sinon.restore();
195-
await client.close();
196-
}
176+
const stats = await client.db('test').stats();
177+
expect(stats).to.exist;
178+
expect(spy.called).to.equal(true);
197179
}
198180
).skipReason = 'todo(NODE-5629): Test passes locally but will fail on CI runs.';
199181

@@ -215,13 +197,12 @@ describe('Authentication Spec Prose Tests', function () {
215197
authMechanism: 'SCRAM-SHA-1'
216198
};
217199

218-
const client = this.configuration.newClient({}, options);
200+
client = this.configuration.newClient({}, options);
219201
const error = await client
220202
.db('test')
221203
.stats()
222204
.catch(e => e);
223205
expect(error.message).to.match(/Authentication failed|SCRAM/);
224-
await client.close();
225206
}
226207
);
227208

@@ -238,13 +219,12 @@ describe('Authentication Spec Prose Tests', function () {
238219
authMechanism: 'SCRAM-SHA-256'
239220
};
240221

241-
const client = this.configuration.newClient({}, options);
222+
client = this.configuration.newClient({}, options);
242223
const error = await client
243224
.db('test')
244225
.stats()
245226
.catch(e => e);
246227
expect(error.message).to.match(/Authentication failed|SCRAM/);
247-
await client.close();
248228
}
249229
);
250230

@@ -310,24 +290,19 @@ describe('Authentication Spec Prose Tests', function () {
310290
authSource: 'admin'
311291
};
312292

293+
client = this.configuration.newClient({}, options);
313294
const commandSpy = sinon.spy(Connection.prototype, 'command');
314-
const client = this.configuration.newClient({}, options);
315-
try {
316-
await client.connect();
317-
const calls = commandSpy
318-
.getCalls()
319-
.filter(c => c.thisValue.id !== '<monitor>') // ignore all monitor connections
320-
.filter(
321-
c => c.args[1][process.env.MONGODB_API_VERSION ? 'hello' : LEGACY_HELLO_COMMAND]
322-
);
323-
324-
expect(calls).to.have.length(1);
325-
const handshakeDoc = calls[0].args[1];
326-
expect(handshakeDoc).to.have.property('speculativeAuthenticate');
327-
} finally {
328-
sinon.restore();
329-
await client.close();
330-
}
295+
await client.connect();
296+
const calls = commandSpy
297+
.getCalls()
298+
.filter(c => c.thisValue.id !== '<monitor>') // ignore all monitor connections
299+
.filter(
300+
c => c.args[1][process.env.MONGODB_API_VERSION ? 'hello' : LEGACY_HELLO_COMMAND]
301+
);
302+
303+
expect(calls).to.have.length(1);
304+
const handshakeDoc = calls[0].args[1];
305+
expect(handshakeDoc).to.have.property('speculativeAuthenticate');
331306
}
332307
);
333308
});
@@ -351,7 +326,8 @@ describe('Authentication Spec Prose Tests', function () {
351326
* mongodb://%E2%85%A8:IV\@mongodb.example.com/admin
352327
* mongodb://%E2%85%A8:I%C2%ADV\@mongodb.example.com/admin
353328
*/
354-
let utilClient;
329+
let utilClient: MongoClient;
330+
let client: MongoClient;
355331
const users = [
356332
{
357333
username: 'IX',
@@ -401,13 +377,9 @@ describe('Authentication Spec Prose Tests', function () {
401377
authMechanism: 'SCRAM-SHA-256'
402378
};
403379

404-
const client = this.configuration.newClient(options);
405-
try {
406-
const stats = await client.db('admin').stats();
407-
expect(stats).to.exist;
408-
} finally {
409-
await client.close();
410-
}
380+
client = this.configuration.newClient(options);
381+
const stats = await client.db('admin').stats();
382+
expect(stats).to.exist;
411383
}
412384
).skipReason = 'todo(NODE-5621): fix the issue with unicode characters.';
413385
}

0 commit comments

Comments
 (0)