From 1a7661885a5ec29a4d9418829439232e080ed983 Mon Sep 17 00:00:00 2001 From: Daria Pardue <81593090+dariakp@users.noreply.github.com> Date: Tue, 31 Aug 2021 17:00:26 -0400 Subject: [PATCH] fix: versioned api low node compat fix (#2970) --- lib/core/index.js | 2 ++ lib/mongo_client.js | 16 ++++++++-------- .../unified-spec-runner/unified-utils.ts | 7 +++++-- test/functional/versioned-api.test.js | 4 +--- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/lib/core/index.js b/lib/core/index.js index 70a3261bc5..3093114d64 100644 --- a/lib/core/index.js +++ b/lib/core/index.js @@ -19,10 +19,12 @@ try { const ServerApiVersion = Object.freeze({ v1: '1' }); +const ValidServerApiVersions = Object.keys(ServerApiVersion).map(key => ServerApiVersion[key]); module.exports = { // Versioned API ServerApiVersion, + ValidServerApiVersions, // Errors MongoError: require('./error').MongoError, MongoNetworkError: require('./error').MongoNetworkError, diff --git a/lib/mongo_client.js b/lib/mongo_client.js index 014c1c6b4a..e9cbbe78ee 100644 --- a/lib/mongo_client.js +++ b/lib/mongo_client.js @@ -5,7 +5,7 @@ const Db = require('./db'); const EventEmitter = require('events').EventEmitter; const inherits = require('util').inherits; const MongoError = require('./core').MongoError; -const ServerApiVersion = require('./core').ServerApiVersion; +const ValidServerApiVersions = require('./core').ValidServerApiVersions; const deprecate = require('util').deprecate; const WriteConcern = require('./write_concern'); const MongoDBNamespace = require('./utils').MongoDBNamespace; @@ -206,16 +206,16 @@ function MongoClient(url, options) { const versionToValidate = serverApiToValidate && serverApiToValidate.version; if (!versionToValidate) { throw new MongoError( - `Invalid \`serverApi\` property; must specify a version from the following enum: ["${Object.values( - ServerApiVersion - ).join('", "')}"]` + `Invalid \`serverApi\` property; must specify a version from the following enum: ["${ValidServerApiVersions.join( + '", "' + )}"]` ); } - if (!Object.values(ServerApiVersion).some(v => v === versionToValidate)) { + if (!ValidServerApiVersions.some(v => v === versionToValidate)) { throw new MongoError( - `Invalid server API version=${versionToValidate}; must be in the following enum: ["${Object.values( - ServerApiVersion - ).join('", "')}"]` + `Invalid server API version=${versionToValidate}; must be in the following enum: ["${ValidServerApiVersions.join( + '", "' + )}"]` ); } options.serverApi = serverApiToValidate; diff --git a/test/functional/unified-spec-runner/unified-utils.ts b/test/functional/unified-spec-runner/unified-utils.ts index 843a97915f..92952450a3 100644 --- a/test/functional/unified-spec-runner/unified-utils.ts +++ b/test/functional/unified-spec-runner/unified-utils.ts @@ -2,7 +2,6 @@ import { expect } from 'chai'; import type { CollectionOrDatabaseOptions, RunOnRequirement, Document } from './schema'; import { gte as semverGte, lte as semverLte } from 'semver'; import { MongoClient } from '../../../index'; -import { isDeepStrictEqual } from 'util'; import { TestConfiguration } from './runner'; export async function topologySatisfies( @@ -41,7 +40,11 @@ export async function topologySatisfies( if (!config.parameters) throw new Error('Configuration does not have server parameters'); for (const [name, value] of Object.entries(r.serverParameters)) { if (name in config.parameters) { - ok &&= isDeepStrictEqual(config.parameters[name], value); + try { + expect(config.parameters[name]).to.deep.equal(value); + } catch (_err) { + ok = false; + } } } } diff --git a/test/functional/versioned-api.test.js b/test/functional/versioned-api.test.js index 523bded7f2..33feee2a7f 100644 --- a/test/functional/versioned-api.test.js +++ b/test/functional/versioned-api.test.js @@ -3,12 +3,11 @@ const expect = require('chai').expect; const loadSpecTests = require('../spec/index').loadSpecTests; const runUnifiedTest = require('./unified-spec-runner/runner').runUnifiedTest; -const ServerApiVersion = require('../../lib/core').ServerApiVersion; +const validVersions = require('../../lib/core').ValidServerApiVersions; describe('Versioned API', function() { describe('client option validation', function() { it('is supported as a client option when it is a valid ServerApiVersion string', function() { - const validVersions = Object.values(ServerApiVersion); expect(validVersions.length).to.be.at.least(1); for (const version of validVersions) { const client = this.configuration.newClient('mongodb://localhost/', { @@ -21,7 +20,6 @@ describe('Versioned API', function() { }); it('is supported as a client option when it is an object with a valid version property', function() { - const validVersions = Object.values(ServerApiVersion); expect(validVersions.length).to.be.at.least(1); for (const version of validVersions) { const client = this.configuration.newClient('mongodb://localhost/', {