diff --git a/src/utils.ts b/src/utils.ts index cf6ad7752d7..4a436c2a35d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -531,9 +531,11 @@ export function resolveOptions( if (writeConcern) { if (timeoutMS != null) { writeConcern = WriteConcern.fromOptions({ - ...writeConcern, - wtimeout: undefined, - wtimeoutMS: undefined + writeConcern: { + ...writeConcern, + wtimeout: undefined, + wtimeoutMS: undefined + } }); } result.writeConcern = writeConcern; diff --git a/test/integration/read-write-concern/write_concern.test.ts b/test/integration/read-write-concern/write_concern.test.ts index 9db7269b89f..58afc2d04e6 100644 --- a/test/integration/read-write-concern/write_concern.test.ts +++ b/test/integration/read-write-concern/write_concern.test.ts @@ -304,4 +304,51 @@ describe('Write Concern', function () { } }); }); + + describe('NODE-6763: write concern is still added with timeoutMS is set', function () { + let client: MongoClient; + let collection: Collection; + const commands: CommandStartedEvent[] = []; + + beforeEach(async function () { + client = this.configuration.newClient({}, { monitorCommands: true }); + client.on('commandStarted', filterForCommands('insert', commands)); + collection = client.db('foo').collection('bar'); + }); + + afterEach(async function () { + await client.close(); + commands.length = 0; + }); + + context('when the write concern includes only timeouts', function () { + it('the writeConcern is not added to the command.', async function () { + await collection.insertOne( + { name: 'john doe' }, + { timeoutMS: 1000, writeConcern: { wtimeout: 1000 } } + ); + const [ + { + command: { writeConcern } + } + ] = commands; + expect(writeConcern).not.to.exist; + }); + }); + + context('when the write concern includes only non-timeout values (`w`)', function () { + it('the writeConcern is added to the command.', async function () { + await collection.insertOne( + { name: 'john doe' }, + { timeoutMS: 1000, writeConcern: { wtimeout: 1000, w: 'majority' } } + ); + const [ + { + command: { writeConcern } + } + ] = commands; + expect(writeConcern).to.deep.equal({ w: 'majority' }); + }); + }); + }); });