From 5dfb62f5d2aa6833a20a72ca3a530cae95a7888c Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Wed, 3 May 2023 12:24:35 -0400 Subject: [PATCH] test: fix issues with cherry-picking #13376 to 6.x --- lib/helpers/processConnectionOptions.js | 7 ++++--- test/connection.test.js | 9 +++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/helpers/processConnectionOptions.js b/lib/helpers/processConnectionOptions.js index a9d862b1030..1dbb767ebee 100644 --- a/lib/helpers/processConnectionOptions.js +++ b/lib/helpers/processConnectionOptions.js @@ -9,11 +9,12 @@ function processConnectionOptions(uri, options) { ? opts.readPreference : getUriReadPreference(uri); + const clonedOpts = clone(opts); const resolvedOpts = (readPreference && readPreference !== 'primary' && readPreference !== 'primaryPreferred') - ? resolveOptsConflicts(readPreference, opts) - : opts; + ? resolveOptsConflicts(readPreference, clonedOpts) + : clonedOpts; - return clone(resolvedOpts); + return resolvedOpts; } function resolveOptsConflicts(pref, opts) { diff --git a/test/connection.test.js b/test/connection.test.js index dcf4cf621c7..49f715a8f4a 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -1537,4 +1537,13 @@ describe('connections:', function() { }); assert.deepEqual(m.connections.length, 0); }); + + describe('processConnectionOptions', function() { + it('should not throw an error when attempting to mutate unmutable options object gh-13335', async function() { + const m = new mongoose.Mongoose(); + const opts = Object.preventExtensions({}); + const conn = await m.connect('mongodb://localhost:27017/db?retryWrites=true&w=majority&readPreference=secondaryPreferred', opts); + assert.ok(conn); + }); + }); });