From 2fb5cc2d93f93a8b503ebc0229fc67333abf5180 Mon Sep 17 00:00:00 2001 From: Warren James Date: Thu, 24 Jun 2021 17:36:10 -0400 Subject: [PATCH 1/2] test: Ensure that tests clean up after themselves --- test/functional/apm.test.js | 80 +++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 35 deletions(-) diff --git a/test/functional/apm.test.js b/test/functional/apm.test.js index ea59f6236ca..6d7d5279978 100644 --- a/test/functional/apm.test.js +++ b/test/functional/apm.test.js @@ -659,41 +659,51 @@ describe('APM', function () { } }); - // NODE-1502 - it('should not allow mutation of internal state from commands returned by event monitoring', function () { - const started = []; - const succeeded = []; - const client = this.configuration.newClient( - { writeConcern: { w: 1 } }, - { maxPoolSize: 1, monitorCommands: true } - ); - client.on('commandStarted', filterForCommands('insert', started)); - client.on('commandSucceeded', filterForCommands('insert', succeeded)); - let documentToInsert = { a: { b: 1 } }; - return client - .connect() - .then(client => { - const db = client.db(this.configuration.db); - return db.collection('apm_test').insertOne(documentToInsert); - }) - .then(r => { - expect(r).to.have.property('insertedId').that.is.an('object'); - expect(started).to.have.lengthOf(1); - // Check if contents of returned document are equal to document inserted (by value) - expect(documentToInsert).to.deep.equal(started[0].command.documents[0]); - // Check if the returned document is a clone of the original. This confirms that the - // reference is not the same. - expect(documentToInsert !== started[0].command.documents[0]).to.equal(true); - expect(documentToInsert.a !== started[0].command.documents[0].a).to.equal(true); - - started[0].command.documents[0].a.b = 2; - expect(documentToInsert.a.b).to.equal(1); - - expect(started[0].commandName).to.equal('insert'); - expect(started[0].command.insert).to.equal('apm_test'); - expect(succeeded).to.have.lengthOf(1); - return client.close(); - }); + describe('Internal state references', function () { + let client; + beforeEach(function (done) { + client = this.configuration.newClient( + { writeConcern: { w: 1 } }, + { maxPoolSize: 1, monitorCommands: true } + ); + done(); + }); + + afterEach(function (done) { + client.close(() => done()); + }); + + // NODE-1502 + it('should not allow mutation of internal state from commands returned by event monitoring', function () { + const started = []; + const succeeded = []; + client.on('commandStarted', filterForCommands('insert', started)); + client.on('commandSucceeded', filterForCommands('insert', succeeded)); + let documentToInsert = { a: { b: 1 } }; + return client + .connect() + .then(client => { + const db = client.db(this.configuration.db); + return db.collection('apm_test').insertOne(documentToInsert); + }) + .then(r => { + expect(r).to.have.property('insertedId').that.is.an('object'); + expect(started).to.have.lengthOf(1); + // Check if contents of returned document are equal to document inserted (by value) + expect(documentToInsert).to.deep.equal(started[0].command.documents[0]); + // Check if the returned document is a clone of the original. This confirms that the + // reference is not the same. + expect(documentToInsert !== started[0].command.documents[0]).to.equal(true); + expect(documentToInsert.a !== started[0].command.documents[0].a).to.equal(true); + + started[0].command.documents[0].a.b = 2; + expect(documentToInsert.a.b).to.equal(1); + + expect(started[0].commandName).to.equal('insert'); + expect(started[0].command.insert).to.equal('apm_test'); + expect(succeeded).to.have.lengthOf(1); + }); + }); }); describe('command monitoring spec tests', function () { From 0f6551e6c09e9954256ca852b760b1e1031bf386 Mon Sep 17 00:00:00 2001 From: Warren James Date: Fri, 25 Jun 2021 10:41:58 -0400 Subject: [PATCH 2/2] style: Remove unneeded callback --- test/functional/apm.test.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/functional/apm.test.js b/test/functional/apm.test.js index 6d7d5279978..432bf46a30d 100644 --- a/test/functional/apm.test.js +++ b/test/functional/apm.test.js @@ -661,16 +661,15 @@ describe('APM', function () { describe('Internal state references', function () { let client; - beforeEach(function (done) { + beforeEach(function () { client = this.configuration.newClient( { writeConcern: { w: 1 } }, { maxPoolSize: 1, monitorCommands: true } ); - done(); }); afterEach(function (done) { - client.close(() => done()); + client.close(done); }); // NODE-1502