@@ -86,6 +86,13 @@ function withTempDb(name, options, client, operation, errorHandler) {
86
86
) ;
87
87
}
88
88
89
+ /**
90
+ * Safely perform a test with provided MongoClient, ensuring client won't leak.
91
+ *
92
+ * @param {MongoClient } client
93
+ * @param {Function|Promise } operation
94
+ * @param {Function|Promise } [errorHandler]
95
+ */
89
96
function withClient ( client , operation , errorHandler ) {
90
97
const cleanup = makeCleanupFn ( client ) ;
91
98
@@ -203,13 +210,29 @@ class EventCollector {
203
210
}
204
211
}
205
212
206
- function withMonitoredClient ( commands , callback ) {
213
+ /**
214
+ * Perform a test with a monitored MongoClient that will filter for certain commands.
215
+ *
216
+ * @param {string|Array } commands commands to filter for
217
+ * @param {object } [options] options to pass on to configuration.newClient
218
+ * @param {object } [options.queryOptions] connection string options
219
+ * @param {object } [options.clientOptions] MongoClient options
220
+ * @param {withMonitoredClientCallback } callback the test function
221
+ */
222
+ function withMonitoredClient ( commands , options , callback ) {
223
+ if ( arguments . length === 2 ) {
224
+ callback = options ;
225
+ options = { } ;
226
+ }
207
227
if ( ! Object . prototype . hasOwnProperty . call ( callback , 'prototype' ) ) {
208
228
throw new Error ( 'withMonitoredClient callback can not be arrow function' ) ;
209
229
}
210
230
return function ( done ) {
211
231
const configuration = this . configuration ;
212
- const client = configuration . newClient ( { monitorCommands : true } ) ;
232
+ const client = configuration . newClient (
233
+ Object . assign ( { } , options . queryOptions ) ,
234
+ Object . assign ( { monitorCommands : true } , options . clientOptions )
235
+ ) ;
213
236
const events = [ ] ;
214
237
client . on ( 'commandStarted' , filterForCommands ( commands , events ) ) ;
215
238
client . connect ( ( err , client ) => {
@@ -222,6 +245,13 @@ function withMonitoredClient(commands, callback) {
222
245
} ;
223
246
}
224
247
248
+ /**
249
+ * @callback withMonitoredClientCallback
250
+ * @param {MongoClient } client monitored client
251
+ * @param {Array } events record of monitored commands
252
+ * @param {Function } done trigger end of test and cleanup
253
+ */
254
+
225
255
module . exports = {
226
256
connectToDb,
227
257
setupDatabase,
0 commit comments