From c0a3b73a4355e837821333d6d9545e9b7e5a1e37 Mon Sep 17 00:00:00 2001 From: Sergey Zelenov Date: Thu, 25 Sep 2025 13:45:50 +0200 Subject: [PATCH 1/2] test(NODE-7184): fix imports & convert into .ts --- .../read_write_concern.spec.test.ts | 2 +- .../{readconcern.test.js => readconcern.test.ts} | 11 ++++++----- .../read-write-concern/write_concern.test.ts | 10 +++++----- 3 files changed, 12 insertions(+), 11 deletions(-) rename test/integration/read-write-concern/{readconcern.test.js => readconcern.test.ts} (98%) diff --git a/test/integration/read-write-concern/read_write_concern.spec.test.ts b/test/integration/read-write-concern/read_write_concern.spec.test.ts index 42c94137d9c..5d4b29fe1e6 100644 --- a/test/integration/read-write-concern/read_write_concern.spec.test.ts +++ b/test/integration/read-write-concern/read_write_concern.spec.test.ts @@ -1,4 +1,4 @@ -import * as path from 'path'; +import * as path from 'node:path'; import { loadSpecTests } from '../../spec'; import { runUnifiedSuite } from '../../tools/unified-spec-runner/runner'; diff --git a/test/integration/read-write-concern/readconcern.test.js b/test/integration/read-write-concern/readconcern.test.ts similarity index 98% rename from test/integration/read-write-concern/readconcern.test.js rename to test/integration/read-write-concern/readconcern.test.ts index 1512b0a4ea8..f4bfa8d41ff 100644 --- a/test/integration/read-write-concern/readconcern.test.js +++ b/test/integration/read-write-concern/readconcern.test.ts @@ -1,9 +1,10 @@ -'use strict'; -const { setupDatabase, filterForCommands } = require('../shared'); -const expect = require('chai').expect; +import { expect } from 'chai'; -describe('ReadConcern', function () { - let client; +import { type MongoClient } from '../../../src'; +import { filterForCommands, setupDatabase } from '../shared'; + +describe.only('ReadConcern', function () { + let client: MongoClient; before(function () { return setupDatabase(this.configuration); diff --git a/test/integration/read-write-concern/write_concern.test.ts b/test/integration/read-write-concern/write_concern.test.ts index d72dd48de0e..4b37055a988 100644 --- a/test/integration/read-write-concern/write_concern.test.ts +++ b/test/integration/read-write-concern/write_concern.test.ts @@ -7,10 +7,10 @@ import { type CommandStartedEvent, type CommandSucceededEvent, type Db, - LEGACY_HELLO_COMMAND, - MongoClient, - OpMsgRequest -} from '../../mongodb'; + MongoClient +} from '../../../src'; +import { OpMsgRequest } from '../../../src/cmap/commands'; +import { LEGACY_HELLO_COMMAND } from '../../../src/constants'; import * as mock from '../../tools/mongodb-mock/index'; import { filterForCommands } from '../shared'; @@ -144,7 +144,7 @@ describe('Write Concern', function () { async test() { const changeStream = col.watch(undefined, { batchSize: 2 }); const changes = on(changeStream, 'change'); - await once(changeStream.cursor, 'init'); + await once(changeStream['cursor'], 'init'); await col.insertMany( [ From bb037fe8c98660a4a9096ef5f7b5f417c7cfe647 Mon Sep 17 00:00:00 2001 From: Sergey Zelenov Date: Thu, 25 Sep 2025 14:00:31 +0200 Subject: [PATCH 2/2] test(NODE-7184): refactor tests --- .../read-write-concern/readconcern.test.ts | 252 ++++++++---------- 1 file changed, 105 insertions(+), 147 deletions(-) diff --git a/test/integration/read-write-concern/readconcern.test.ts b/test/integration/read-write-concern/readconcern.test.ts index f4bfa8d41ff..96c1e39ca85 100644 --- a/test/integration/read-write-concern/readconcern.test.ts +++ b/test/integration/read-write-concern/readconcern.test.ts @@ -1,9 +1,9 @@ import { expect } from 'chai'; -import { type MongoClient } from '../../../src'; +import { type MongoClient, ReadConcernLevel } from '../../../src'; import { filterForCommands, setupDatabase } from '../shared'; -describe.only('ReadConcern', function () { +describe('ReadConcern', function () { let client: MongoClient; before(function () { @@ -29,27 +29,27 @@ describe.only('ReadConcern', function () { { description: 'Should set local readConcern on db level when using collection method', commandName: 'find', - readConcern: { level: 'local' } + readConcern: { level: ReadConcernLevel.local } }, { description: 'Should set majority readConcern on db level', commandName: 'find', - readConcern: { level: 'majority' } + readConcern: { level: ReadConcernLevel.majority } }, { description: 'Should set majority readConcern aggregate command', commandName: 'aggregate', - readConcern: { level: 'majority' } + readConcern: { level: ReadConcernLevel.majority } }, { description: 'Should set local readConcern at collection level', commandName: 'find', - readConcern: { level: 'local' } + readConcern: { level: ReadConcernLevel.local } }, { description: 'Should set majority readConcern at collection level', commandName: 'find', - readConcern: { level: 'majority' } + readConcern: { level: ReadConcernLevel.majority } } ]; @@ -57,7 +57,7 @@ describe.only('ReadConcern', function () { it(test.description, { metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } }, - test: function (done) { + test: async function () { const started = []; const succeeded = []; // Get a new instance @@ -67,37 +67,29 @@ describe.only('ReadConcern', function () { { maxPoolSize: 1, readConcern: test.readConcern, monitorCommands: true } ); - client.connect((err, client) => { - expect(err).to.not.exist; + await client.connect(); - const db = client.db(configuration.db); - expect(db.readConcern).to.deep.equal(test.readConcern); + const db = client.db(configuration.db); + expect(db.readConcern).to.deep.equal(test.readConcern); - // Get a collection - const collection = db.collection('readConcernCollection'); + // Get a collection + const collection = db.collection('readConcernCollection'); - // Validate readConcern - expect(collection.readConcern).to.deep.equal(test.readConcern); + // Validate readConcern + expect(collection.readConcern).to.deep.equal(test.readConcern); - // commandMonitoring - client.on('commandStarted', filterForCommands(test.commandName, started)); - client.on('commandSucceeded', filterForCommands(test.commandName, succeeded)); + // commandMonitoring + client.on('commandStarted', filterForCommands(test.commandName, started)); + client.on('commandSucceeded', filterForCommands(test.commandName, succeeded)); - // Execute find - if (test.commandName === 'find') { - collection.find().toArray(err => { - expect(err).to.not.exist; - validateTestResults(started, succeeded, test.commandName, test.readConcern.level); - done(); - }); - } else if (test.commandName === 'aggregate') { - collection.aggregate([{ $match: {} }]).toArray(err => { - expect(err).to.not.exist; - validateTestResults(started, succeeded, test.commandName, test.readConcern.level); - done(); - }); - } - }); + // Execute find + if (test.commandName === 'find') { + await collection.find().toArray(); + validateTestResults(started, succeeded, test.commandName, test.readConcern.level); + } else if (test.commandName === 'aggregate') { + await collection.aggregate([{ $match: {} }]).toArray(); + validateTestResults(started, succeeded, test.commandName, test.readConcern.level); + } } }); }); @@ -107,23 +99,23 @@ describe.only('ReadConcern', function () { { description: 'Should set local readConcern using MongoClient', urlReadConcernLevel: 'readConcernLevel=local', - readConcern: { level: 'local' } + readConcern: { level: ReadConcernLevel.local } }, { description: 'Should set majority readConcern using MongoClient', urlReadConcernLevel: 'readConcernLevel=majority', - readConcern: { level: 'majority' } + readConcern: { level: ReadConcernLevel.majority } }, { description: 'Should set majority readConcern using MongoClient with options', - readConcern: { level: 'majority' } + readConcern: { level: ReadConcernLevel.majority } } ]; urlTests.forEach(test => { it(test.description, { metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } }, - test: function (done) { + test: async function () { const started = []; const succeeded = []; // Get a new instance @@ -143,29 +135,24 @@ describe.only('ReadConcern', function () { }); } - client.connect((err, client) => { - expect(err).to.not.exist; + await client.connect(); - const db = client.db(configuration.db); - expect(db.readConcern).to.deep.equal(test.readConcern); + const db = client.db(configuration.db); + expect(db.readConcern).to.deep.equal(test.readConcern); - // Get a collection - const collection = db.collection('readConcernCollection'); + // Get a collection + const collection = db.collection('readConcernCollection'); - // Validate readConcern - expect(collection.readConcern).to.deep.equal(test.readConcern); + // Validate readConcern + expect(collection.readConcern).to.deep.equal(test.readConcern); - // commandMonitoring - client.on('commandStarted', filterForCommands('find', started)); - client.on('commandSucceeded', filterForCommands('find', succeeded)); + // commandMonitoring + client.on('commandStarted', filterForCommands('find', started)); + client.on('commandSucceeded', filterForCommands('find', succeeded)); - // Execute find - collection.find().toArray(err => { - expect(err).to.not.exist; - validateTestResults(started, succeeded, 'find', test.readConcern.level); - done(); - }); - }); + // Execute find + await collection.find().toArray(); + validateTestResults(started, succeeded, 'find', test.readConcern.level); } }); }); @@ -176,13 +163,13 @@ describe.only('ReadConcern', function () { description: 'Should set majority readConcern distinct command', commandName: 'distinct', mongodbVersion: '>= 3.2', - readConcern: { level: 'majority' } + readConcern: { level: ReadConcernLevel.majority } }, { description: 'Should set majority readConcern count command', commandName: 'count', mongodbVersion: '>= 3.2', - readConcern: { level: 'majority' } + readConcern: { level: ReadConcernLevel.majority } } ]; @@ -190,7 +177,7 @@ describe.only('ReadConcern', function () { it(test.description, { metadata: { requires: { topology: 'replicaset', mongodb: test.mongodbVersion } }, - test: function (done) { + test: async function () { const started = []; const succeeded = []; // Get a new instance @@ -200,50 +187,39 @@ describe.only('ReadConcern', function () { { maxPoolSize: 1, readConcern: test.readConcern, monitorCommands: true } ); - client.connect((err, client) => { - expect(err).to.not.exist; - - const db = client.db(configuration.db); - expect(db.readConcern).to.deep.equal(test.readConcern); + await client.connect(); - // Get the collection - const collection = db.collection('readConcernCollection'); + const db = client.db(configuration.db); + expect(db.readConcern).to.deep.equal(test.readConcern); + + // Get the collection + const collection = db.collection('readConcernCollection'); + + // Insert documents to perform distinct against + await collection.insertMany( + [ + { a: 0, b: { c: 'a' } }, + { a: 1, b: { c: 'b' } }, + { a: 1, b: { c: 'c' } }, + { a: 2, b: { c: 'a' } }, + { a: 3 }, + { a: 3 } + ], + configuration.writeConcernMax() + ); - // Insert documents to perform distinct against - collection.insertMany( - [ - { a: 0, b: { c: 'a' } }, - { a: 1, b: { c: 'b' } }, - { a: 1, b: { c: 'c' } }, - { a: 2, b: { c: 'a' } }, - { a: 3 }, - { a: 3 } - ], - configuration.writeConcernMax(), - err => { - expect(err).to.not.exist; - - // Listen to apm events - client.on('commandStarted', filterForCommands(test.commandName, started)); - client.on('commandSucceeded', filterForCommands(test.commandName, succeeded)); - - // Perform a distinct query against the a field - if (test.commandName === 'distinct') { - collection.distinct('a', err => { - expect(err).to.not.exist; - validateTestResults(started, succeeded, test.commandName, test.readConcern.level); - done(); - }); - } else if (test.commandName === 'count') { - collection.estimatedDocumentCount(err => { - expect(err).to.not.exist; - validateTestResults(started, succeeded, test.commandName, test.readConcern.level); - done(); - }); - } - } - ); - }); + // Listen to apm events + client.on('commandStarted', filterForCommands(test.commandName, started)); + client.on('commandSucceeded', filterForCommands(test.commandName, succeeded)); + + // Perform a distinct query against the a field + if (test.commandName === 'distinct') { + await collection.distinct('a'); + validateTestResults(started, succeeded, test.commandName, test.readConcern.level); + } else if (test.commandName === 'count') { + await collection.estimatedDocumentCount(); + validateTestResults(started, succeeded, test.commandName, test.readConcern.level); + } } }); }); @@ -251,7 +227,7 @@ describe.only('ReadConcern', function () { it('Should set majority readConcern aggregate command against server >= 4.1', { metadata: { requires: { topology: 'replicaset' } }, - test: function (done) { + test: async function () { const started = []; const succeeded = []; // Get a new instance @@ -261,65 +237,47 @@ describe.only('ReadConcern', function () { { maxPoolSize: 1, readConcern: { level: 'majority' }, monitorCommands: true } ); - client - .connect() - .then(() => { - // Get a collection - const collection = client - .db(configuration.db) - .collection('readConcernCollectionAggregate1'); + await client.connect(); + // Get a collection + const collection = client.db(configuration.db).collection('readConcernCollectionAggregate1'); - // Listen to apm events - client.on('commandStarted', filterForCommands('aggregate', started)); - client.on('commandSucceeded', filterForCommands('aggregate', succeeded)); + // Listen to apm events + client.on('commandStarted', filterForCommands('aggregate', started)); + client.on('commandSucceeded', filterForCommands('aggregate', succeeded)); - // Execute find - return collection - .aggregate([{ $match: {} }, { $out: 'readConcernCollectionAggregate1Output' }]) - .toArray() - .then(() => { - validateTestResults(started, succeeded, 'aggregate', 'majority'); - - // Execute find - return collection - .aggregate([{ $match: {} }], { out: 'readConcernCollectionAggregate2Output' }) - .toArray() - .then(() => { - validateTestResults(started, succeeded, 'aggregate', 'majority'); - }); - }); - }) - .then( - () => client.close(done), - e => client.close(() => done(e)) - ); + // Execute find + await collection + .aggregate([{ $match: {} }, { $out: 'readConcernCollectionAggregate1Output' }]) + .toArray(); + + validateTestResults(started, succeeded, 'aggregate', 'majority'); + + // Execute find + await collection + .aggregate([{ $match: {} }], { out: 'readConcernCollectionAggregate2Output' }) + .toArray(); + validateTestResults(started, succeeded, 'aggregate', 'majority'); } }); it('Should set local readConcern on db level when using createCollection method', { metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } }, - test: function (done) { + test: async function () { // Get a new instance const configuration = this.configuration; client = configuration.newClient( { w: 1 }, { maxPoolSize: 1, readConcern: { level: 'local' } } ); - client.connect((err, client) => { - expect(err).to.not.exist; - const db = client.db(configuration.db); - expect(db.readConcern).to.deep.equal({ level: 'local' }); - - // Get a collection using createCollection - db.createCollection('readConcernCollection_createCollection', (err, collection) => { - expect(err).to.not.exist; - - // Validate readConcern - expect(collection.readConcern).to.deep.equal({ level: 'local' }); - done(); - }); - }); + await client.connect(); + const db = client.db(configuration.db); + expect(db.readConcern).to.deep.equal({ level: 'local' }); + + // Get a collection using createCollection + const collection = await db.createCollection('readConcernCollection_createCollection'); + // Validate readConcern + expect(collection.readConcern).to.deep.equal({ level: 'local' }); } }); });