diff --git a/test/integration/command-logging-and-monitoring/command_monitoring.test.ts b/test/integration/command-logging-and-monitoring/command_monitoring.test.ts index 124d5e7f9a2..7841dfc5b0f 100644 --- a/test/integration/command-logging-and-monitoring/command_monitoring.test.ts +++ b/test/integration/command-logging-and-monitoring/command_monitoring.test.ts @@ -1,6 +1,6 @@ import { expect } from 'chai'; -import { ReadPreference } from '../../mongodb'; +import { type MongoClient, ReadPreference } from '../../mongodb'; import { filterForCommands, ignoreNsNotFound, setupDatabase } from '../shared'; describe('Command Monitoring', function () { @@ -8,6 +8,12 @@ describe('Command Monitoring', function () { return setupDatabase(this.configuration); }); + let client: MongoClient; + + afterEach(async function () { + await client?.close(); + }); + it('should correctly receive the APM events for an insert', { metadata: { requires: { topology: ['single', 'replicaset', 'sharded'] } }, @@ -98,47 +104,34 @@ describe('Command Monitoring', function () { } }); - it('should correctly receive the APM events for a listIndexes command', { - metadata: { requires: { topology: ['replicaset'], mongodb: '>=3.0.0' } }, - - test: function () { - const started = []; - const succeeded = []; - const client = this.configuration.newClient( - { writeConcern: { w: 1 } }, - { maxPoolSize: 1, monitorCommands: true } - ); - - const desiredEvents = ['listIndexes', 'find']; - client.on('commandStarted', filterForCommands(desiredEvents, started)); - client.on('commandSucceeded', filterForCommands(desiredEvents, succeeded)); - - const db = client.db(this.configuration.db); - - return db - .collection('apm_test_list_collections') - .insertOne({ a: 1 }, this.configuration.writeConcernMax()) - .then(r => { - expect(r).property('insertedId').to.exist; - - return db - .collection('apm_test_list_collections') - .listIndexes({ readPreference: ReadPreference.PRIMARY }) - .toArray(); - }) - .then(() => - db - .collection('apm_test_list_collections') - .listIndexes({ readPreference: ReadPreference.SECONDARY }) - .toArray() - ) - .then(() => { - expect(started).to.have.lengthOf(2); - expect(started[0]).property('address').to.not.equal(started[1].address); - - return client.close(); - }); - } + it('should correctly receive the APM events for a listIndexes command', async function () { + const started = []; + const succeeded = []; + client = this.configuration.newClient( + { writeConcern: { w: 1 } }, + { maxPoolSize: 1, monitorCommands: true } + ); + + client.on('commandStarted', filterForCommands('listIndexes', started)); + client.on('commandSucceeded', filterForCommands('listIndexes', succeeded)); + + const db = client.db(this.configuration.db); + + await db + .collection('apm_test_list_collections') + .insertOne({ a: 1 }, this.configuration.writeConcernMax()); + + await db + .collection('apm_test_list_collections') + .listIndexes({ readPreference: ReadPreference.PRIMARY }) + .toArray(); + + await db + .collection('apm_test_list_collections') + .listIndexes({ readPreference: ReadPreference.SECONDARY }) + .toArray(); + expect(started).to.have.lengthOf(2); + expect(started[0]).property('address').to.not.equal(started[1].address); }); it('should correctly receive the APM events for a find with getmore and killcursor', {