Skip to content

Commit

Permalink
fix(MongoMemoryServer::getStartOptions): coerce version to semver bef…
Browse files Browse the repository at this point in the history
…ore semver checks

fixes #841
  • Loading branch information
hasezoey committed Jan 4, 2024
1 parent f5c7e27 commit 427531e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/mongodb-memory-server-core/src/MongoMemoryServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import debug from 'debug';
import { EventEmitter } from 'events';
import { promises as fspromises } from 'fs';
import { AddUserOptions, MongoClient } from 'mongodb';
import { InstanceInfoError, StateError } from './util/errors';
import { InstanceInfoError, StateError, UnknownVersionError } from './util/errors';
import * as os from 'os';
import { DryMongoBinary } from './util/DryMongoBinary';
import * as semver from 'semver';
Expand Down Expand Up @@ -372,8 +372,17 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
const opts = await DryMongoBinary.generateOptions(this.opts.binary);
let storageEngine = instOpts.storageEngine;

// try to convert a string to a valid semver, like "v6.0-latest" (compiles to "6.0.0")
// use "0.0.0" as a fallback to have a valid semver for later checks, but warn on invalid
const coercedVersion = semver.coerce(opts.version) ?? new semver.SemVer('0.0.0');

// warn on invalid version here, a invalid version will be thrown in MongoBinaryDownloadUrl if downloading
if (semver.eq(coercedVersion, '0.0.0')) {
console.warn(new UnknownVersionError(opts.version));
}

// warn when storage engine "ephemeralForTest" is explicitly used and switch to "wiredTiger"
if (storageEngine === 'ephemeralForTest' && semver.gte(opts.version, '7.0.0')) {
if (storageEngine === 'ephemeralForTest' && semver.gte(coercedVersion, '7.0.0')) {
console.warn(
'Storage Engine "ephemeralForTest" is removed since mongodb 7.0.0, automatically using "wiredTiger"!\n' +
'This warning is because the mentioned storage engine is explicitly used and mongodb version is 7.0.0 or higher'
Expand All @@ -383,7 +392,7 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
}

if (isNullOrUndefined(storageEngine)) {
if (semver.gte(opts.version, '7.0.0')) {
if (semver.gte(coercedVersion, '7.0.0')) {
storageEngine = 'wiredTiger';
} else {
storageEngine = 'ephemeralForTest';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,24 @@ describe('MongoMemoryServer', () => {

await utils.removeDir(options.data.tmpDir!); // manual cleanup
});

it('should work with -latest versions [#841]', async () => {
const mongoServer = new MongoMemoryServer({ binary: { version: 'v6.0-latest' } });

// @ts-expect-error "getStartOptions" is protected
await mongoServer.getStartOptions();
});

it('should work with -latest versions [#841]', async () => {
const spy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {});
const mongoServer = new MongoMemoryServer({ binary: { version: 'junk' } });

// @ts-expect-error "getStartOptions" is protected
await mongoServer.getStartOptions();

expect(console.warn).toHaveBeenCalledTimes(1);
expect(spy.mock.calls).toMatchSnapshot();
});
});

it('"getDbPath" should return the dbPath', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ This may be because of using a v6.x way of calling functions, look at the follow
https://nodkz.github.io/mongodb-memory-server/docs/guides/migration/migrate7#no-function-other-than-start-create-ensureinstance-will-be-starting-anything"
`;

exports[`MongoMemoryServer getStartOptions() should work with -latest versions [#841] 1`] = `
Array [
Array [
[Error: Could not coerce VERSION to a semver version (version: "junk")],
],
]
`;

exports[`MongoMemoryServer getUri() should throw an state error, if not starting or running 1`] = `
"Incorrect State for operation: \\"new\\", allowed States: \\"[running,starting]\\"
This may be because of using a v6.x way of calling functions, look at the following guide if anything applies:
Expand Down

0 comments on commit 427531e

Please sign in to comment.