From bf1667443ae4851a16e380561545cf44d2841acb Mon Sep 17 00:00:00 2001 From: Sergey Zelenov Date: Mon, 20 Oct 2025 15:17:36 +0200 Subject: [PATCH 1/2] test(NODE-7254): refactor callback-style test to async/await --- .../node-specific/operation_examples.test.ts | 101 ++++++------------ 1 file changed, 33 insertions(+), 68 deletions(-) diff --git a/test/integration/node-specific/operation_examples.test.ts b/test/integration/node-specific/operation_examples.test.ts index b6ea6bd09ff..e9dbbefcc18 100644 --- a/test/integration/node-specific/operation_examples.test.ts +++ b/test/integration/node-specific/operation_examples.test.ts @@ -1,16 +1,11 @@ import { expect } from 'chai'; -import { - Code, - enumToString, - type MongoClient, - ProfilingLevel, - ReturnDocument -} from '../../mongodb'; +import { Code, type MongoClient, ProfilingLevel, ReturnDocument } from '../../../src'; +import { enumToString } from '../../../src/utils'; import { sleep as delay } from '../../tools/utils'; import { setupDatabase } from '../shared'; -describe('Operations', function () { +describe.only('Operations', function () { let client: MongoClient; beforeEach(async function () { @@ -3989,77 +3984,47 @@ describe('Operations', function () { }); /** - * A simple example showing the listening to a capped collection using a Promise. + * A simple example showing the listening to a capped collection. * * example-class Db * example-method createCollection */ - it('Should correctly add capped collection options to cursor With Promises', { - metadata: { requires: { topology: ['single'] } }, - - test: function (done) { - const configuration = this.configuration; - const client = configuration.newClient(configuration.writeConcernMax(), { - maxPoolSize: 1 - }); - - client.connect().then(function (client) { - const db = client.db(configuration.db); - // LINE var MongoClient = require('mongodb').MongoClient, - // LINE test = require('assert'); - // LINE const client = new MongoClient('mongodb://localhost:27017/test'); - // LINE client.connect().then(() => { - // LINE var db = client.db('test); - // REPLACE configuration.writeConcernMax() WITH {w:1} - // REMOVE-LINE done(); - // BEGIN - // Create a capped collection with a maximum of 1000 documents - let collection; - - db.createCollection('a_simple_collection_2_with_promise', { - capped: true, - size: 100000, - max: 1000, - writeConcern: { w: 1 } - }) - .then(function (_collection) { - collection = _collection; - - const docs: Array<{ a: number }> = []; - for (let i = 0; i < 1000; i++) docs.push({ a: i }); + it('Should correctly add capped collection options to cursor', async function () { + const configuration = this.configuration; + const client = configuration.newClient(configuration.writeConcernMax(), { + maxPoolSize: 1 + }); - // Insert a document in the capped collection - return collection.insertMany(docs, configuration.writeConcernMax()); - }) - .then(function (result) { - expect(result).to.exist; + await client.connect(); + const db = client.db(configuration.db); - let total = 0; + const collection = await db.createCollection('a_simple_collection_2_with_promise', { + capped: true, + size: 100000, + max: 1000, + writeConcern: { w: 1 } + }); + const docs: Array<{ a: number }> = []; + for (let i = 0; i < 10000; i++) docs.push({ a: i }); - // Get the cursor - const cursor = collection - .find({ a: { $gte: 0 } }) - .addCursorFlag('tailable', true) - .addCursorFlag('awaitData', true); + // Insert a document in the capped collection + await collection.insertMany(docs, configuration.writeConcernMax()); + let total = 0; - const stream = cursor.stream(); - stream.on('data', function (d) { - expect(d).to.exist; - total = total + 1; + // Get the cursor + const cursor = collection + .find({ a: { $gte: 0 } }) + .addCursorFlag('tailable', true) + .addCursorFlag('awaitData', true); - if (total === 1000) { - cursor.close(); - } - }); + const stream = cursor.stream(); - cursor.on('close', function () { - // TODO: forced because the cursor is still open/active - client.close(true, done); - }); - }); - }); - // END + for await (const d of stream) { + expect(d).to.have.property('_id'); + total = total + 1; + if (total === 1000) await cursor.close(); } + await client.close(); }); describe('Transaction Examples', function () { From d219d4550afc015b7099f77e45ea9de757077ecb Mon Sep 17 00:00:00 2001 From: Sergey Zelenov Date: Tue, 21 Oct 2025 10:17:16 +0200 Subject: [PATCH 2/2] test(NODE-7254): run all tests --- test/integration/node-specific/operation_examples.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/node-specific/operation_examples.test.ts b/test/integration/node-specific/operation_examples.test.ts index e9dbbefcc18..d8e07af5462 100644 --- a/test/integration/node-specific/operation_examples.test.ts +++ b/test/integration/node-specific/operation_examples.test.ts @@ -5,7 +5,7 @@ import { enumToString } from '../../../src/utils'; import { sleep as delay } from '../../tools/utils'; import { setupDatabase } from '../shared'; -describe.only('Operations', function () { +describe('Operations', function () { let client: MongoClient; beforeEach(async function () {