Skip to content

Commit 952609b

Browse files
committed
feat(MongoMemoryServer): use storage engine "wiredTiger" if version is 7.0.0 or higher
fixes #742
1 parent f099597 commit 952609b

File tree

8 files changed

+70
-5
lines changed

8 files changed

+70
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ const mongod = new MongoMemoryServer({
122122
ip?: string, // by default '127.0.0.1', for binding to all IP addresses set it to `::,0.0.0.0`,
123123
dbName?: string, // by default '' (empty string)
124124
dbPath?: string, // by default create in temp directory
125-
storageEngine?: string, // by default `ephemeralForTest`, available engines: [ 'ephemeralForTest', 'wiredTiger' ]
125+
storageEngine?: string, // by default `ephemeralForTest`(unless mongodb 7.0.0, where its `wiredTiger`), available engines: [ 'ephemeralForTest', 'wiredTiger' ]
126126
replSet?: string, // by default no replica set, replica set name
127127
args?: string[], // by default no additional arguments, any additional command line arguments for `mongod` `mongod` (ex. ['--notablescan'])
128128
},

docs/api/interfaces/mongo-memory-instance-opts.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ This option will automatically be set with a directory generated by [`mkdtemp`](
7272
### storageEngine
7373

7474
Typings: `storageEngine?: StorageEngine`
75-
Default: `ephemeralForTest`
75+
Default: `ephemeralForTest` (unless mongodb version is `7.0.0`, where its `wiredTiger`)
7676

7777
Set which storage engine to use, uses [`StorageEngine`](#helper-type-storageengine).
7878

@@ -98,4 +98,6 @@ Custom Explanation:
9898
MongoDB has stated that storage-engine `ephemeralForTest` is unstable and has been deprecated, it will be removed with mongodb 7.
9999

100100
In MMS there has been no observation of `ephemeralForTest` being unstable (aside from some missing features) and will continue to be the default until mongodb-memory-server changes to a version where `ephemeralForTest` is not present anymore.
101+
102+
With mongodb-memory-server 9.0.0, if mongodb 7.0.0 or higher is used, `wiredTiger` is the default engine.
101103
:::

docs/api/interfaces/replset-opts.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Also see [`MongoMemoryInstanceOpts.spawn`](./mongo-memory-instance-opts.md#spawn
6666
### storageEngine
6767

6868
Typings: `storageEngine?: StorageEngine`
69-
Default: `"ephemeralForTest"`
69+
Default: `"ephemeralForTest"` (unless mongodb version is `7.0.0`, where its `wiredTiger`)
7070

7171
Set which Storage Engine to use, uses [`StorageEngine`](./mongo-memory-instance-opts.md#helper-type-storageengine).
7272

docs/guides/migration/migrate9.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,10 @@ This *should* help with non-closed instances not exiting the nodejs process.
109109
Previously MMS used `get-port`, but it caused some big memory-leakage across big projects, so it has been replaced with one that uses less maps.
110110

111111
It also has been replaced because newer versions were ESM only, but we couldnt switch to ESM yet (and using ESM in CommonJS is not a great experience)
112+
113+
## Mongodb version 7.0.0 is now supported
114+
115+
Mongob version `7.0.0` removed storage engine `ephemeralForTest`, with mongodb-memory-server 9.0.0 storage engine `wiredTiger` is the default for binary versions `7.0.0` and higher.
116+
Older versions (before `7.0.0`) will still continue to use `ephemeralForTest` by default.
117+
118+
It is recommended to run those instances with a db path which is equivalent to [`tmpfs`](https://wiki.archlinux.org/title/tmpfs).

packages/mongodb-memory-server-core/src/MongoMemoryReplSet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export interface ReplSetOpts {
8181
spawn?: SpawnOptions;
8282
/**
8383
*`mongod` storage engine type
84-
* @default 'ephemeralForTest'
84+
* @default 'ephemeralForTest' unless mongodb version is `7.0.0`, where its `wiredTiger`
8585
*/
8686
storageEngine?: StorageEngine;
8787
/**

packages/mongodb-memory-server-core/src/MongoMemoryServer.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { promises as fspromises } from 'fs';
2020
import { AddUserOptions, MongoClient } from 'mongodb';
2121
import { InstanceInfoError, StateError } from './util/errors';
2222
import * as os from 'os';
23+
import { DryMongoBinary } from './util/DryMongoBinary';
24+
import * as semver from 'semver';
2325

2426
const log = debug('MongoMS:MongoMemoryServer');
2527

@@ -375,12 +377,33 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
375377
port = await this.getNewPort(port);
376378
}
377379

380+
const opts = await DryMongoBinary.generateOptions(this.opts.binary);
381+
let storageEngine = instOpts.storageEngine;
382+
383+
// warn when storage engine "ephemeralForTest" is explicitly used and switch to "wiredTiger"
384+
if (storageEngine === 'ephemeralForTest' && semver.gte(opts.version, '7.0.0')) {
385+
console.warn(
386+
'Storage Engine "ephemeralForTest" is removed since mongodb 7.0.0, automatically using "wiredTiger"!\n' +
387+
'This warning is because the mentioned storage engine is explicitly used and mongodb version is 7.0.0 or higher'
388+
);
389+
390+
storageEngine = 'wiredTiger';
391+
}
392+
393+
if (isNullOrUndefined(storageEngine)) {
394+
if (semver.gte(opts.version, '7.0.0')) {
395+
storageEngine = 'wiredTiger';
396+
} else {
397+
storageEngine = 'ephemeralForTest';
398+
}
399+
}
400+
378401
// consider directly using "this.opts.instance", to pass through all options, even if not defined in "StartupInstanceData"
379402
const data: StartupInstanceData = {
380403
port: port,
381404
dbName: generateDbName(instOpts.dbName),
382405
ip: instOpts.ip ?? '127.0.0.1',
383-
storageEngine: instOpts.storageEngine ?? 'ephemeralForTest',
406+
storageEngine: storageEngine,
384407
replSet: instOpts.replSet,
385408
dbPath: instOpts.dbPath,
386409
tmpDir: undefined,

packages/mongodb-memory-server-core/src/__tests__/MongoMemoryServer.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,4 +1113,28 @@ describe('MongoMemoryServer', () => {
11131113
mongoServer._instanceInfo.tmpDir!
11141114
); // manual cleanup
11151115
});
1116+
1117+
describe('server version specific', () => {
1118+
// should use default options that are supported for 7.0 (like not using "ephemeralForTest" by default)
1119+
it('should allow mongodb by default 7.0', async () => {
1120+
const server = await MongoMemoryServer.create({ binary: { version: '7.0.0' } });
1121+
1122+
await server.stop();
1123+
});
1124+
1125+
it('should warn if "ephemeralForTest" is used explicitly in mongodb 7.0', async () => {
1126+
const spy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {});
1127+
const server = await MongoMemoryServer.create({
1128+
binary: { version: '7.0.0' },
1129+
instance: { storageEngine: 'ephemeralForTest' },
1130+
});
1131+
1132+
expect(console.warn).toHaveBeenCalledTimes(1);
1133+
expect(spy.mock.calls).toMatchSnapshot();
1134+
1135+
expect(server.instanceInfo?.storageEngine).toStrictEqual('wiredTiger');
1136+
1137+
await server.stop();
1138+
});
1139+
});
11161140
});

packages/mongodb-memory-server-core/src/__tests__/__snapshots__/MongoMemoryServer.test.ts.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ This may be because of using a v6.x way of calling functions, look at the follow
3636
https://nodkz.github.io/mongodb-memory-server/docs/guides/migration/migrate7#no-function-other-than-start-create-ensureinstance-will-be-starting-anything"
3737
`;
3838

39+
exports[`MongoMemoryServer server version specific should warn if "ephemeralForTest" is used explicitly in mongodb 7.0 1`] = `
40+
Array [
41+
Array [
42+
"Storage Engine \\"ephemeralForTest\\" is removed since mongodb 7.0.0, automatically using \\"wiredTiger\\"!
43+
This warning is because the mentioned storage engine is explicitly used and mongodb version is 7.0.0 or higher",
44+
],
45+
]
46+
`;
47+
3948
exports[`MongoMemoryServer start() should throw an error if state is not "new" or "stopped" 1`] = `
4049
"Incorrect State for operation: \\"starting\\", allowed States: \\"[new,stopped]\\"
4150
This may be because of using a v6.x way of calling functions, look at the following guide if anything applies:

0 commit comments

Comments
 (0)