Skip to content

Commit 21a0769

Browse files
jloveridgenodkz
authored andcommitted
feat(MongoInstance): add new options auth, replSet and args for providing additional mongod args (#79) thanks @jloveridge
* feat(MongoInstance): add new option `instance.args` to allow passing additional args. feat(MongoInstance): add new option `instance.auth` to allow running with auth enabled. feat(MongoInstance): add new option `instance.replSet` to specify replica set name. * docs(readme): Update README with info about new options for instance. * chore(instanceData): The instance data should have `replSet` details on it. * chore(type): Augment the types with information about the new options.
1 parent edfa536 commit 21a0769

File tree

6 files changed

+78
-3
lines changed

6 files changed

+78
-3
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const mongod = new MongodbMemoryServer({
5353
dbPath?: string, // by default create in temp directory
5454
storageEngine?: string, // by default `ephemeralForTest`, available engines: [ 'devnull', 'ephemeralForTest', 'mmapv1', 'wiredTiger' ]
5555
debug?: boolean, // by default false
56+
replSet?: string, // by default no replica set, replica set name
57+
auth?: boolean, // by default `mongod` is started with '--noauth', start `mongod` with '--auth'
58+
args?: string[], // by default no additional arguments, any additional command line arguments for `mongod` `mongod` (ex. ['--notablescan'])
5659
},
5760
binary: {
5861
version?: string, // by default 'latest'

src/MongoMemoryServer.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export interface MongoMemoryServerOptsT {
1010
dbName?: string;
1111
storageEngine?: string;
1212
debug?: boolean | ((...args: any[]) => any);
13+
replSet?: string;
14+
auth?: boolean;
15+
args?: string[];
1316
};
1417
binary: {
1518
version?: string;
@@ -35,6 +38,7 @@ export interface MongoInstanceDataT {
3538
name: string;
3639
removeCallback: ((...args: any[]) => any);
3740
};
41+
replSet?: string;
3842
}
3943

4044
export default class MongoMemoryServer {

src/MongoMemoryServer.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export type MongoMemoryServerOptsT = {
1717
dbName?: string,
1818
storageEngine?: string,
1919
debug?: boolean | Function,
20+
replSet?: string,
21+
args?: string[],
22+
auth?: boolean,
2023
},
2124
binary: {
2225
version?: string,
@@ -42,6 +45,7 @@ export type MongoInstanceDataT = {
4245
name: string,
4346
removeCallback: Function,
4447
},
48+
replSet?: string,
4549
};
4650

4751
async function generateDbName(dbName?: string): Promise<string> {
@@ -116,6 +120,7 @@ export default class MongoMemoryServer {
116120
data.dbName = await generateDbName(instOpts.dbName);
117121
data.uri = await generateConnectionString(data.port, data.dbName);
118122
data.storageEngine = instOpts.storageEngine || 'ephemeralForTest';
123+
data.replSet = instOpts.replSet;
119124
if (instOpts.dbPath) {
120125
data.dbPath = instOpts.dbPath;
121126
} else {
@@ -133,6 +138,9 @@ export default class MongoMemoryServer {
133138
storageEngine: data.storageEngine,
134139
dbPath: data.dbPath,
135140
debug: this.opts.instance.debug,
141+
replSet: data.replSet,
142+
args: this.opts.instance.args,
143+
auth: this.opts.instance.auth,
136144
},
137145
binary: this.opts.binary,
138146
spawn: this.opts.spawn,

src/util/MongoInstance.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export interface MongodOps {
1010
storageEngine?: string;
1111
dbPath: string;
1212
debug?: boolean | ((...args: any[]) => any);
13+
replSet?: string;
14+
args?: string[];
15+
auth?: boolean;
1316
};
1417

1518
// mongo binary options

src/util/MongoInstance.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export type MongodOps = {
1414
storageEngine?: string,
1515
dbPath: string,
1616
debug?: boolean | Function,
17+
replSet?: string,
18+
args?: string[],
19+
auth?: boolean,
1720
},
1821

1922
// mongo binary options
@@ -75,16 +78,17 @@ export default class MongodbInstance {
7578
}
7679

7780
prepareCommandArgs(): string[] {
78-
const { ip, port, storageEngine, dbPath } = this.opts.instance;
81+
const { ip, port, storageEngine, dbPath, replSet, auth, args } = this.opts.instance;
7982

8083
const result = [];
8184
result.push('--bind_ip', ip || '127.0.0.1');
8285
if (port) result.push('--port', port.toString());
8386
if (storageEngine) result.push('--storageEngine', storageEngine);
8487
if (dbPath) result.push('--dbpath', dbPath);
85-
result.push('--noauth');
88+
if (!auth) result.push('--noauth');
89+
if (replSet) result.push('--replSet', replSet);
8690

87-
return result;
91+
return result.concat(args || []);
8892
}
8993

9094
async run(): Promise<MongodbInstance> {

src/util/__tests__/MongoInstance-test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,59 @@ describe('MongoInstance', () => {
3737
]);
3838
});
3939

40+
it('should allow specifying replSet', () => {
41+
const inst = new MongoInstance({
42+
instance: {
43+
port: 27555,
44+
dbPath: '/data',
45+
replSet: 'testset',
46+
},
47+
});
48+
expect(inst.prepareCommandArgs()).toEqual([
49+
'--bind_ip',
50+
'127.0.0.1',
51+
'--port',
52+
'27555',
53+
'--dbpath',
54+
'/data',
55+
'--noauth',
56+
'--replSet',
57+
'testset',
58+
]);
59+
});
60+
61+
it('should be able to enable auth', () => {
62+
const inst = new MongoInstance({
63+
instance: {
64+
port: 27555,
65+
dbPath: '/data',
66+
auth: true,
67+
},
68+
});
69+
expect(inst.prepareCommandArgs()).toEqual([
70+
'--bind_ip',
71+
'127.0.0.1',
72+
'--port',
73+
'27555',
74+
'--dbpath',
75+
'/data',
76+
]);
77+
});
78+
79+
it('should be able to pass arbitrary args', () => {
80+
const args = ['--notablescan', '--nounixsocket'];
81+
const inst = new MongoInstance({
82+
instance: {
83+
port: 27555,
84+
dbPath: '/data',
85+
args,
86+
},
87+
});
88+
expect(inst.prepareCommandArgs()).toEqual(
89+
['--bind_ip', '127.0.0.1', '--port', '27555', '--dbpath', '/data', '--noauth'].concat(args)
90+
);
91+
});
92+
4093
it('should start instance on port 27333', async () => {
4194
const mongod = await MongoInstance.run({
4295
instance: { port: 27333, dbPath: tmpDir.name },

0 commit comments

Comments
 (0)