From c70cf110109f1c6c1b05a59e1b02deec18cc889f Mon Sep 17 00:00:00 2001 From: Sergey Zelenov Date: Thu, 16 Oct 2025 13:13:05 +0200 Subject: [PATCH 1/2] test(NODE-7205): migrate to typescript --- .../node-specific/{db.test.js => db.test.ts} | 61 +++++++++---------- 1 file changed, 30 insertions(+), 31 deletions(-) rename test/integration/node-specific/{db.test.js => db.test.ts} (77%) diff --git a/test/integration/node-specific/db.test.js b/test/integration/node-specific/db.test.ts similarity index 77% rename from test/integration/node-specific/db.test.js rename to test/integration/node-specific/db.test.ts index a092a8d888b..e129a3fe301 100644 --- a/test/integration/node-specific/db.test.js +++ b/test/integration/node-specific/db.test.ts @@ -1,8 +1,7 @@ -'use strict'; +import { expect } from 'chai'; -const { setupDatabase, assert: test } = require(`../shared`); -const { expect } = require('chai'); -const { MongoClient, MongoInvalidArgumentError, MongoServerError } = require('../../mongodb'); +import { type Db, MongoClient, MongoInvalidArgumentError, MongoServerError } from '../../mongodb'; +import { assert as test, setupDatabase } from '../shared'; describe('Db', function () { before(function () { @@ -10,8 +9,8 @@ describe('Db', function () { }); context('when given illegal db name', function () { - let client; - let db; + let client: MongoClient; + let db: Db; beforeEach(function () { client = this.configuration.newClient(); @@ -59,10 +58,10 @@ describe('Db', function () { }, test: function (done) { - var configuration = this.configuration; - var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); + const configuration = this.configuration; + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); client.connect(function (err, client) { - var _db = client.db('nonexistingdb'); + const _db = client.db('nonexistingdb'); _db.dropDatabase(function (err, result) { expect(err).to.not.exist; @@ -80,8 +79,8 @@ describe('Db', function () { }, test: function (done) { - var configuration = this.configuration; - var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); + const configuration = this.configuration; + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); client.connect(err => { expect(err).to.not.exist; @@ -101,12 +100,12 @@ describe('Db', function () { }, test: function (done) { - var configuration = this.configuration; - var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); + const configuration = this.configuration; + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); client.connect(function (err, client) { expect(err).to.not.exist; - var db1 = client.db('node972'); + const db1 = client.db('node972'); db1.collection('node972.test').insertOne({ a: 1 }, function (err) { expect(err).to.not.exist; @@ -129,14 +128,14 @@ describe('Db', function () { }, test: function (done) { - var configuration = this.configuration; + const configuration = this.configuration; - var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); client.connect(function (err, client) { expect(err).to.not.exist; // Get a db we that does not have any collections - var db1 = client.db('shouldCorrectlyUseCursorWithListCollectionsCommand'); + const db1 = client.db('shouldCorrectlyUseCursorWithListCollectionsCommand'); // Create a collection db1.collection('test').insertOne({ a: 1 }, function (err) { @@ -147,7 +146,7 @@ describe('Db', function () { expect(err).to.not.exist; // Get listCollections filtering out the name - var cursor = db1.listCollections({ name: 'test1' }); + const cursor = db1.listCollections({ name: 'test1' }); cursor.toArray(function (err, names) { expect(err).to.not.exist; test.equal(1, names.length); @@ -166,14 +165,14 @@ describe('Db', function () { }, test: function (done) { - var configuration = this.configuration; + const configuration = this.configuration; - var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); client.connect(function (err, client) { expect(err).to.not.exist; // Get a db we that does not have any collections - var db1 = client.db('shouldCorrectlyUseCursorWithListCollectionsCommandAndBatchSize'); + const db1 = client.db('shouldCorrectlyUseCursorWithListCollectionsCommandAndBatchSize'); // Create a collection db1.collection('test').insertOne({ a: 1 }, function (err) { @@ -184,7 +183,7 @@ describe('Db', function () { expect(err).to.not.exist; // Get listCollections filtering out the name - var cursor = db1.listCollections({ name: 'test' }, { batchSize: 1 }); + const cursor = db1.listCollections({ name: 'test' }, { batchSize: 1 }); cursor.toArray(function (err, names) { expect(err).to.not.exist; test.equal(1, names.length); @@ -203,14 +202,14 @@ describe('Db', function () { }, test: function (done) { - var configuration = this.configuration; + const configuration = this.configuration; - var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); client.connect(function (err, client) { expect(err).to.not.exist; // Get a db we that does not have any collections - var db1 = client.db('shouldCorrectlyListCollectionsWithDotsOnThem'); + const db1 = client.db('shouldCorrectlyListCollectionsWithDotsOnThem'); // Create a collection db1.collection('test.collection1').insertOne({ a: 1 }, function (err) { @@ -221,13 +220,13 @@ describe('Db', function () { expect(err).to.not.exist; // Get listCollections filtering out the name - var cursor = db1.listCollections({ name: /test.collection/ }); + const cursor = db1.listCollections({ name: /test.collection/ }); cursor.toArray(function (err, names) { expect(err).to.not.exist; test.equal(2, names.length); // Get listCollections filtering out the name - var cursor = db1.listCollections({ name: 'test.collection1' }, {}); + const cursor = db1.listCollections({ name: 'test.collection1' }, {}); cursor.toArray(function (err, names) { expect(err).to.not.exist; test.equal(1, names.length); @@ -250,14 +249,14 @@ describe('Db', function () { }, test: function (done) { - var configuration = this.configuration; + const configuration = this.configuration; - var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); client.connect(function (err, client) { expect(err).to.not.exist; // Get a db we that does not have any collections - var db1 = client.db('shouldCorrectlyListCollectionsWithDotsOnThemFor28'); + const db1 = client.db('shouldCorrectlyListCollectionsWithDotsOnThemFor28'); // Create a collection db1.collection('test.collection1').insertOne({ a: 1 }, function (err) { @@ -268,7 +267,7 @@ describe('Db', function () { expect(err).to.not.exist; // Get listCollections filtering out the name - var cursor = db1.listCollections({ name: /test.collection/ }, { batchSize: 1 }); + const cursor = db1.listCollections({ name: /test.collection/ }, { batchSize: 1 }); cursor.toArray(function (err, names) { expect(err).to.not.exist; test.equal(2, names.length); From f9551c679a7d627b6fa74529bb56a11c4f299268 Mon Sep 17 00:00:00 2001 From: Sergey Zelenov Date: Thu, 16 Oct 2025 13:33:31 +0200 Subject: [PATCH 2/2] test(NODE-7205): refactor tests --- test/integration/node-specific/db.test.ts | 303 +++++++--------------- 1 file changed, 98 insertions(+), 205 deletions(-) diff --git a/test/integration/node-specific/db.test.ts b/test/integration/node-specific/db.test.ts index e129a3fe301..50f1cddd255 100644 --- a/test/integration/node-specific/db.test.ts +++ b/test/integration/node-specific/db.test.ts @@ -1,7 +1,13 @@ import { expect } from 'chai'; -import { type Db, MongoClient, MongoInvalidArgumentError, MongoServerError } from '../../mongodb'; -import { assert as test, setupDatabase } from '../shared'; +import { + Collection, + type Db, + MongoClient, + MongoInvalidArgumentError, + MongoServerError +} from '../../../src'; +import { setupDatabase } from '../shared'; describe('Db', function () { before(function () { @@ -52,236 +58,123 @@ describe('Db', function () { expect(error).to.be.instanceOf(Error); }); - it('shouldCorrectlyGetErrorDroppingNonExistingDb', { - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded'] } - }, - - test: function (done) { - const configuration = this.configuration; - const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); - client.connect(function (err, client) { - const _db = client.db('nonexistingdb'); - - _db.dropDatabase(function (err, result) { - expect(err).to.not.exist; - test.equal(true, result); - - client.close(done); - }); - }); - } + it('should not throw error dropping non existing Db', async function () { + const configuration = this.configuration; + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); + const _db = client.db('nonexistingdb'); + await _db.dropDatabase(); + await client.close(); }); - it.skip('shouldCorrectlyThrowWhenTryingToReOpenConnection', { - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded'] } - }, - - test: function (done) { - const configuration = this.configuration; - const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); - client.connect(err => { - expect(err).to.not.exist; - - try { - client.connect(function () {}); - test.ok(false); - } catch { - client.close(done); - } - }); - } + // TODO(NODE-7192): remove test as it doesn't test anything + // it.skip('shouldCorrectlyThrowWhenTryingToReOpenConnection', { + // metadata: { + // requires: { topology: ['single', 'replicaset', 'sharded'] } + // }, + // + // test: function (done) { + // var configuration = this.configuration; + // var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); + // client.connect(err => { + // expect(err).to.not.exist; + // + // try { + // client.connect(function () {}); + // test.ok(false); + // } catch { + // client.close(done); + // } + // }); + // } + // }); + + it('should not cut collection name when it is the same as the database', async function () { + const configuration = this.configuration; + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); + const db1 = client.db('node972'); + await db1.collection('node972.test').insertOne({ a: 1 }); + + const collections = await db1.collections(); + const collection = collections.find(c => c.collectionName === 'node972.test'); + expect(collection).to.be.instanceOf(Collection); + await client.close(); }); - it('should not cut collection name when it is the same as the database', { - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded'] } - }, - - test: function (done) { - const configuration = this.configuration; - const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); - client.connect(function (err, client) { - expect(err).to.not.exist; - - const db1 = client.db('node972'); - db1.collection('node972.test').insertOne({ a: 1 }, function (err) { - expect(err).to.not.exist; - - db1.collections(function (err, collections) { - expect(err).to.not.exist; - collections = collections.map(function (c) { - return c.collectionName; - }); - test.notEqual(-1, collections.indexOf('node972.test')); - client.close(done); - }); - }); - }); - } - }); + it('should correctly use cursor with list collections command', async function () { + const configuration = this.configuration; + + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); - it('shouldCorrectlyUseCursorWithListCollectionsCommand', { - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded'] } - }, + const db1 = client.db('shouldCorrectlyUseCursorWithListCollectionsCommand'); - test: function (done) { - const configuration = this.configuration; + // create 2 collections by inserting documents in them + await db1.collection('test').insertOne({ a: 1 }); + await db1.collection('test1').insertOne({ a: 1 }); - const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); - client.connect(function (err, client) { - expect(err).to.not.exist; + // Get listCollections filtering out the name + const collections = await db1.listCollections({ name: 'test1' }).toArray(); + expect(collections.length).to.equal(1); + await client.close(); + }); - // Get a db we that does not have any collections - const db1 = client.db('shouldCorrectlyUseCursorWithListCollectionsCommand'); + it('should correctly use cursor with listCollections command and batchSize', async function () { + const configuration = this.configuration; - // Create a collection - db1.collection('test').insertOne({ a: 1 }, function (err) { - expect(err).to.not.exist; + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); + const db1 = client.db('shouldCorrectlyUseCursorWithListCollectionsCommandAndBatchSize'); - // Create a collection - db1.collection('test1').insertOne({ a: 1 }, function () { - expect(err).to.not.exist; + await db1.collection('test').insertOne({ a: 1 }); - // Get listCollections filtering out the name - const cursor = db1.listCollections({ name: 'test1' }); - cursor.toArray(function (err, names) { - expect(err).to.not.exist; - test.equal(1, names.length); + await db1.collection('test1').insertOne({ a: 1 }); - client.close(done); - }); - }); - }); - }); - } + // Get listCollections filtering out the name + const collections = await db1.listCollections({ name: 'test' }, { batchSize: 1 }).toArray(); + expect(collections.length).to.equal(1); + await client.close(); }); - it('shouldCorrectlyUseCursorWithListCollectionsCommandAndBatchSize', { - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded'] } - }, + it('should correctly list collection names with . in the middle', async function () { + const configuration = this.configuration; - test: function (done) { - const configuration = this.configuration; + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); + const db1 = client.db('shouldCorrectlyListCollectionsWithDotsOnThem'); - const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); - client.connect(function (err, client) { - expect(err).to.not.exist; + await db1.collection('test.collection1').insertOne({ a: 1 }); + await db1.collection('test.collection2').insertOne({ a: 1 }); - // Get a db we that does not have any collections - const db1 = client.db('shouldCorrectlyUseCursorWithListCollectionsCommandAndBatchSize'); + const collections = await db1.listCollections({ name: /test.collection/ }).toArray(); + expect(collections.length).to.equal(2); - // Create a collection - db1.collection('test').insertOne({ a: 1 }, function (err) { - expect(err).to.not.exist; + // Get listCollections filtering out the name + const filteredCollections = await db1 + .listCollections({ name: 'test.collection1' }, {}) + .toArray(); + expect(filteredCollections.length).to.equal(1); + await client.close(); + }); - // Create a collection - db1.collection('test1').insertOne({ a: 1 }, function () { - expect(err).to.not.exist; + it('should correctly list collection names with batchSize 1', async function () { + const configuration = this.configuration; - // Get listCollections filtering out the name - const cursor = db1.listCollections({ name: 'test' }, { batchSize: 1 }); - cursor.toArray(function (err, names) { - expect(err).to.not.exist; - test.equal(1, names.length); + const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); + const db1 = client.db('shouldCorrectlyListCollectionsWithDotsOnThemFor28'); - client.close(done); - }); - }); - }); - }); - } - }); + await db1.collection('test.collection1').insertOne({ a: 1 }); - it('should correctly list collection names with . in the middle', { - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded'] } - }, - - test: function (done) { - const configuration = this.configuration; - - const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); - client.connect(function (err, client) { - expect(err).to.not.exist; - - // Get a db we that does not have any collections - const db1 = client.db('shouldCorrectlyListCollectionsWithDotsOnThem'); - - // Create a collection - db1.collection('test.collection1').insertOne({ a: 1 }, function (err) { - expect(err).to.not.exist; - - // Create a collection - db1.collection('test.collection2').insertOne({ a: 1 }, function () { - expect(err).to.not.exist; - - // Get listCollections filtering out the name - const cursor = db1.listCollections({ name: /test.collection/ }); - cursor.toArray(function (err, names) { - expect(err).to.not.exist; - test.equal(2, names.length); - - // Get listCollections filtering out the name - const cursor = db1.listCollections({ name: 'test.collection1' }, {}); - cursor.toArray(function (err, names) { - expect(err).to.not.exist; - test.equal(1, names.length); - - client.close(done); - }); - }); - }); - }); - }); - } - }); + await db1.collection('test.collection2').insertOne({ a: 1 }); - it('should correctly list collection names with batchSize 1 for 2.8 or higher', { - metadata: { - requires: { - topology: ['single', 'replicaset', 'sharded'], - mongodb: '>= 2.8.0' - } - }, - - test: function (done) { - const configuration = this.configuration; - - const client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); - client.connect(function (err, client) { - expect(err).to.not.exist; - - // Get a db we that does not have any collections - const db1 = client.db('shouldCorrectlyListCollectionsWithDotsOnThemFor28'); - - // Create a collection - db1.collection('test.collection1').insertOne({ a: 1 }, function (err) { - expect(err).to.not.exist; - - // Create a collection - db1.collection('test.collection2').insertOne({ a: 1 }, function () { - expect(err).to.not.exist; - - // Get listCollections filtering out the name - const cursor = db1.listCollections({ name: /test.collection/ }, { batchSize: 1 }); - cursor.toArray(function (err, names) { - expect(err).to.not.exist; - test.equal(2, names.length); - - client.close(done); - }); - }); - }); - }); - } + // Get listCollections filtering out the name + const collections = await db1 + .listCollections({ name: /test.collection/ }, { batchSize: 1 }) + .toArray(); + expect(collections.length).to.equal(2); + await client.close(); }); it('should throw if Db.collection is passed a deprecated callback argument', () => { const client = new MongoClient('mongodb://iLoveJavascript'); + // @ts-expect-error Not allowed in TS, but can be used in JS + // eslint-disable-next-line @typescript-eslint/no-empty-function expect(() => client.db('test').collection('test', () => {})).to.throw( 'The callback form of this helper has been removed.' );