mongodb-memory-server 2.1.0 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +64 -2
  2. package/lib/MongoMemoryReplSet.d.ts +57 -0
  3. package/lib/MongoMemoryReplSet.js +307 -0
  4. package/lib/MongoMemoryReplSet.js.flow +252 -0
  5. package/lib/MongoMemoryServer.d.ts +47 -0
  6. package/lib/MongoMemoryServer.js +76 -78
  7. package/lib/MongoMemoryServer.js.flow +22 -26
  8. package/lib/__tests__/multipleDB-test.js +68 -0
  9. package/lib/__tests__/replset-test.js +87 -0
  10. package/lib/__tests__/singleDB-test.js +43 -0
  11. package/lib/index.d.ts +6 -0
  12. package/lib/index.js +32 -14
  13. package/lib/index.js.flow +2 -1
  14. package/lib/types.d.ts +49 -0
  15. package/lib/types.js +1 -0
  16. package/lib/types.js.flow +60 -0
  17. package/lib/util/MongoBinary.d.ts +23 -0
  18. package/lib/util/MongoBinary.js +46 -42
  19. package/lib/util/MongoBinary.js.flow +13 -6
  20. package/lib/util/MongoBinaryDownload.d.ts +33 -0
  21. package/lib/util/MongoBinaryDownload.js +84 -73
  22. package/lib/util/MongoBinaryDownload.js.flow +5 -11
  23. package/lib/util/MongoBinaryDownloadUrl.d.ts +35 -0
  24. package/lib/util/MongoBinaryDownloadUrl.js +68 -31
  25. package/lib/util/MongoBinaryDownloadUrl.js.flow +12 -17
  26. package/lib/util/MongoInstance.d.ts +49 -0
  27. package/lib/util/MongoInstance.js +50 -32
  28. package/lib/util/MongoInstance.js.flow +12 -17
  29. package/lib/util/__tests__/MongoBinary-test.js +52 -0
  30. package/lib/util/__tests__/MongoBinaryDownload-test.js +42 -0
  31. package/lib/util/__tests__/MongoBinaryDownloadUrl-test.js +179 -0
  32. package/lib/util/__tests__/MongoInstance-test.js +133 -0
  33. package/lib/util/db_util.d.ts +20 -0
  34. package/lib/util/db_util.js +56 -0
  35. package/lib/util/db_util.js.flow +29 -0
  36. package/lib/util/mongo_killer.d.ts +0 -0
  37. package/lib/util/mongo_killer.js +5 -5
  38. package/package.json +36 -23
@@ -0,0 +1,47 @@
1
+ /// <reference types='node' />
2
+
3
+ import { ChildProcess, SpawnOptions } from 'child_process';
4
+ import MongoInstance from './util/MongoInstance';
5
+ import { MongoBinaryOpts } from './util/MongoBinary';
6
+ import { CallbackFn, DebugFn, MongoMemoryInstancePropT, StorageEngineT } from './types';
7
+
8
+ export interface MongoMemoryServerOptsT {
9
+ instance: MongoMemoryInstancePropT;
10
+ binary: MongoBinaryOpts;
11
+ debug?: boolean;
12
+ spawn: SpawnOptions;
13
+ autoStart?: boolean;
14
+ }
15
+
16
+ export interface MongoInstanceDataT {
17
+ port: number;
18
+ dbPath: string;
19
+ dbName: string;
20
+ uri: string;
21
+ storageEngine: StorageEngineT;
22
+ instance: MongoInstance;
23
+ childProcess: ChildProcess;
24
+ tmpDir?: {
25
+ name: string;
26
+ removeCallback: CallbackFn;
27
+ };
28
+ replSet?: string;
29
+ }
30
+
31
+ export default class MongoMemoryServer {
32
+ isRunning: boolean;
33
+ runningInstance: Promise<MongoInstanceDataT> | undefined;
34
+ opts: MongoMemoryServerOptsT;
35
+ debug: DebugFn;
36
+
37
+ constructor(opts?: Partial<MongoMemoryServerOptsT>);
38
+
39
+ start(): Promise<boolean>;
40
+ stop(): Promise<boolean>;
41
+ getInstanceData(): Promise<MongoInstanceDataT>;
42
+ getUri(otherDbName?: string | boolean): Promise<string>;
43
+ getConnectionString(otherDbName?: string | boolean): Promise<string>;
44
+ getPort(): Promise<number>;
45
+ getDbPath(): Promise<string>;
46
+ getDbName(): Promise<string>;
47
+ }
@@ -1,59 +1,50 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.default = void 0;
6
7
 
7
- let generateDbName = (() => {
8
- var _ref = _asyncToGenerator(function* (dbName) {
9
- return dbName || (0, _v2.default)();
10
- });
11
-
12
- return function generateDbName(_x) {
13
- return _ref.apply(this, arguments);
14
- };
15
- })();
16
-
17
- let generateConnectionString = (() => {
18
- var _ref2 = _asyncToGenerator(function* (port, dbName) {
19
- return `mongodb://127.0.0.1:${port}/${dbName}`;
20
- });
21
-
22
- return function generateConnectionString(_x2, _x3) {
23
- return _ref2.apply(this, arguments);
24
- };
25
- })();
8
+ var _tmp = _interopRequireDefault(require("tmp"));
26
9
 
27
- var _v = require('uuid/v4');
10
+ var _getPort = _interopRequireDefault(require("get-port"));
28
11
 
29
- var _v2 = _interopRequireDefault(_v);
12
+ var _debug = _interopRequireDefault(require("debug"));
30
13
 
31
- var _tmp = require('tmp');
14
+ var _db_util = require("./util/db_util");
32
15
 
33
- var _tmp2 = _interopRequireDefault(_tmp);
16
+ var _MongoInstance = _interopRequireDefault(require("./util/MongoInstance"));
34
17
 
35
- var _getPort = require('get-port');
18
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
36
19
 
37
- var _getPort2 = _interopRequireDefault(_getPort);
20
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
38
21
 
39
- var _debug = require('debug');
22
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
40
23
 
41
- var _debug2 = _interopRequireDefault(_debug);
24
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
42
25
 
43
- var _MongoInstance = require('./util/MongoInstance');
26
+ _tmp.default.setGracefulCleanup();
44
27
 
45
- var _MongoInstance2 = _interopRequireDefault(_MongoInstance);
28
+ function generateConnectionString(_x, _x2) {
29
+ return _generateConnectionString.apply(this, arguments);
30
+ }
46
31
 
47
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
32
+ function _generateConnectionString() {
33
+ _generateConnectionString = _asyncToGenerator(function* (port, dbName) {
34
+ return `mongodb://127.0.0.1:${port}/${dbName}`;
35
+ });
36
+ return _generateConnectionString.apply(this, arguments);
37
+ }
48
38
 
49
- function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
39
+ class MongoMemoryServer {
40
+ constructor(opts = {}) {
41
+ _defineProperty(this, "isRunning", false);
50
42
 
51
- _tmp2.default.setGracefulCleanup();
43
+ _defineProperty(this, "runningInstance", void 0);
52
44
 
53
- class MongoMemoryServer {
45
+ _defineProperty(this, "opts", void 0);
54
46
 
55
- constructor(opts = {}) {
56
- this.isRunning = false;
47
+ _defineProperty(this, "debug", void 0);
57
48
 
58
49
  this.opts = opts;
59
50
  if (!this.opts.instance) this.opts.instance = {};
@@ -63,9 +54,9 @@ class MongoMemoryServer {
63
54
  if (this.opts.debug) {
64
55
  console.log(msg);
65
56
  }
66
- };
57
+ }; // autoStart by default
58
+
67
59
 
68
- // autoStart by default
69
60
  if (!opts.hasOwnProperty('autoStart') || opts.autoStart) {
70
61
  this.debug('Autostarting MongoDB instance...');
71
62
  this.start();
@@ -80,23 +71,23 @@ class MongoMemoryServer {
80
71
  throw new Error('MongoDB instance already in status startup/running/error. Use opts.debug = true for more info.');
81
72
  }
82
73
 
83
- _this.runningInstance = _this._startUpInstance().catch(function (err) {
74
+ _this.runningInstance = _this._startUpInstance().catch(err => {
84
75
  if (err.message === 'Mongod shutting down' || err === 'Mongod shutting down') {
85
76
  _this.debug(`Mongodb does not started. Trying to start on another port one more time...`);
77
+
86
78
  _this.opts.instance.port = null;
87
79
  return _this._startUpInstance();
88
80
  }
81
+
89
82
  throw err;
90
- }).catch(function (err) {
83
+ }).catch(err => {
91
84
  if (!_this.opts.debug) {
92
85
  throw new Error(`${err.message}\n\nUse debug option for more info: ` + `new MongoMemoryServer({ debug: true })`);
93
86
  }
94
- throw err;
95
- });
96
87
 
97
- return _this.runningInstance.then(function () {
98
- return true;
88
+ throw err;
99
89
  });
90
+ return _this.runningInstance.then(() => true);
100
91
  })();
101
92
  }
102
93
 
@@ -106,31 +97,40 @@ class MongoMemoryServer {
106
97
  return _asyncToGenerator(function* () {
107
98
  const data = {};
108
99
  let tmpDir;
109
-
110
100
  const instOpts = _this2.opts.instance;
111
- data.port = yield (0, _getPort2.default)({ port: instOpts.port });
112
- _this2.debug = (0, _debug2.default)(`Mongo[${data.port}]`);
101
+ data.port = yield (0, _getPort.default)({
102
+ port: instOpts.port
103
+ });
104
+ _this2.debug = (0, _debug.default)(`Mongo[${data.port}]`);
113
105
  _this2.debug.enabled = !!_this2.opts.debug;
114
- data.dbName = yield generateDbName(instOpts.dbName);
106
+ data.dbName = (0, _db_util.generateDbName)(instOpts.dbName);
115
107
  data.uri = yield generateConnectionString(data.port, data.dbName);
116
108
  data.storageEngine = instOpts.storageEngine || 'ephemeralForTest';
109
+ data.replSet = instOpts.replSet;
110
+
117
111
  if (instOpts.dbPath) {
118
112
  data.dbPath = instOpts.dbPath;
119
113
  } else {
120
- tmpDir = _tmp2.default.dirSync({ prefix: 'mongo-mem-', unsafeCleanup: true });
114
+ tmpDir = _tmp.default.dirSync({
115
+ prefix: 'mongo-mem-',
116
+ unsafeCleanup: true
117
+ });
121
118
  data.dbPath = tmpDir.name;
122
119
  }
123
120
 
124
- _this2.debug(`Starting MongoDB instance with following options: ${JSON.stringify(data)}`);
125
-
126
- // Download if not exists mongo binaries in ~/.mongodb-prebuilt
121
+ _this2.debug(`Starting MongoDB instance with following options: ${JSON.stringify(data)}`); // Download if not exists mongo binaries in ~/.mongodb-prebuilt
127
122
  // After that startup MongoDB instance
128
- const instance = yield _MongoInstance2.default.run({
123
+
124
+
125
+ const instance = yield _MongoInstance.default.run({
129
126
  instance: {
130
127
  port: data.port,
131
128
  storageEngine: data.storageEngine,
132
129
  dbPath: data.dbPath,
133
- debug: _this2.opts.instance.debug
130
+ debug: _this2.opts.instance.debug,
131
+ replSet: data.replSet,
132
+ args: _this2.opts.instance.args,
133
+ auth: _this2.opts.instance.auth
134
134
  },
135
135
  binary: _this2.opts.binary,
136
136
  spawn: _this2.opts.spawn,
@@ -139,7 +139,6 @@ class MongoMemoryServer {
139
139
  data.instance = instance;
140
140
  data.childProcess = instance.childProcess;
141
141
  data.tmpDir = tmpDir;
142
-
143
142
  return data;
144
143
  })();
145
144
  }
@@ -148,18 +147,18 @@ class MongoMemoryServer {
148
147
  var _this3 = this;
149
148
 
150
149
  return _asyncToGenerator(function* () {
151
- var _ref3 = yield _this3.getInstanceData();
152
-
153
- const instance = _ref3.instance,
154
- port = _ref3.port,
155
- tmpDir = _ref3.tmpDir;
156
-
150
+ const _ref = yield _this3.getInstanceData(),
151
+ instance = _ref.instance,
152
+ port = _ref.port,
153
+ tmpDir = _ref.tmpDir;
157
154
 
158
155
  _this3.debug(`Shutdown MongoDB server on port ${port} with pid ${instance.getPid() || ''}`);
156
+
159
157
  yield instance.kill();
160
158
 
161
159
  if (tmpDir) {
162
160
  _this3.debug(`Removing tmpDir ${tmpDir.name}`);
161
+
163
162
  tmpDir.removeCallback();
164
163
  }
165
164
 
@@ -175,6 +174,7 @@ class MongoMemoryServer {
175
174
  if (_this4.runningInstance) {
176
175
  return _this4.runningInstance;
177
176
  }
177
+
178
178
  throw new Error('Database instance is not running. You should start database by calling start() method. BTW it should start automatically if opts.autoStart!=false. Also you may provide opts.debug=true for more info.');
179
179
  })();
180
180
  }
@@ -183,20 +183,19 @@ class MongoMemoryServer {
183
183
  var _this5 = this;
184
184
 
185
185
  return _asyncToGenerator(function* () {
186
- var _ref4 = yield _this5.getInstanceData();
187
-
188
- const uri = _ref4.uri,
189
- port = _ref4.port;
186
+ const _ref2 = yield _this5.getInstanceData(),
187
+ uri = _ref2.uri,
188
+ port = _ref2.port; // IF true OR string
190
189
 
191
- // IF true OR string
192
190
 
193
191
  if (otherDbName) {
194
192
  if (typeof otherDbName === 'string') {
195
193
  // generate uri with provided DB name on existed DB instance
196
194
  return generateConnectionString(port, otherDbName);
197
- }
198
- // generate new random db name
199
- return generateConnectionString(port, (yield generateDbName()));
195
+ } // generate new random db name
196
+
197
+
198
+ return generateConnectionString(port, (0, _db_util.generateDbName)());
200
199
  }
201
200
 
202
201
  return uri;
@@ -215,9 +214,8 @@ class MongoMemoryServer {
215
214
  var _this7 = this;
216
215
 
217
216
  return _asyncToGenerator(function* () {
218
- var _ref5 = yield _this7.getInstanceData();
219
-
220
- const port = _ref5.port;
217
+ const _ref3 = yield _this7.getInstanceData(),
218
+ port = _ref3.port;
221
219
 
222
220
  return port;
223
221
  })();
@@ -227,9 +225,8 @@ class MongoMemoryServer {
227
225
  var _this8 = this;
228
226
 
229
227
  return _asyncToGenerator(function* () {
230
- var _ref6 = yield _this8.getInstanceData();
231
-
232
- const dbPath = _ref6.dbPath;
228
+ const _ref4 = yield _this8.getInstanceData(),
229
+ dbPath = _ref4.dbPath;
233
230
 
234
231
  return dbPath;
235
232
  })();
@@ -239,12 +236,13 @@ class MongoMemoryServer {
239
236
  var _this9 = this;
240
237
 
241
238
  return _asyncToGenerator(function* () {
242
- var _ref7 = yield _this9.getInstanceData();
243
-
244
- const dbName = _ref7.dbName;
239
+ const _ref5 = yield _this9.getInstanceData(),
240
+ dbName = _ref5.dbName;
245
241
 
246
242
  return dbName;
247
243
  })();
248
244
  }
245
+
249
246
  }
247
+
250
248
  exports.default = MongoMemoryServer;
@@ -1,32 +1,27 @@
1
1
  /* @flow */
2
2
 
3
3
  import type { ChildProcess } from 'child_process';
4
- import uuid from 'uuid/v4';
5
4
  import tmp from 'tmp';
6
5
  import getport from 'get-port';
7
6
  import Debug from 'debug';
7
+ import { generateDbName } from './util/db_util';
8
8
  import MongoInstance from './util/MongoInstance';
9
+ import type { MongoBinaryOpts } from './util/MongoBinary';
10
+ import type {
11
+ CallbackFn,
12
+ DebugFn,
13
+ MongoMemoryInstancePropT,
14
+ SpawnOptions,
15
+ StorageEngineT,
16
+ } from './types';
9
17
 
10
18
  tmp.setGracefulCleanup();
11
19
 
12
20
  export type MongoMemoryServerOptsT = {
13
- instance: {
14
- port?: ?number,
15
- ip?: string, // for binding to all IP addresses set it to `::,0.0.0.0`, by default '127.0.0.1'
16
- dbPath?: string,
17
- dbName?: string,
18
- storageEngine?: string,
19
- debug?: boolean | Function,
20
- },
21
- binary: {
22
- version?: string,
23
- downloadDir?: string,
24
- platform?: string,
25
- arch?: string,
26
- debug?: boolean | Function,
27
- },
21
+ instance: MongoMemoryInstancePropT,
22
+ binary: MongoBinaryOpts,
28
23
  debug?: boolean,
29
- spawn: any,
24
+ spawn: SpawnOptions,
30
25
  autoStart?: boolean,
31
26
  };
32
27
 
@@ -35,19 +30,16 @@ export type MongoInstanceDataT = {
35
30
  dbPath: string,
36
31
  dbName: string,
37
32
  uri: string,
38
- storageEngine: string,
33
+ storageEngine: StorageEngineT,
39
34
  instance: MongoInstance,
40
35
  childProcess: ChildProcess,
41
36
  tmpDir?: {
42
37
  name: string,
43
- removeCallback: Function,
38
+ removeCallback: CallbackFn,
44
39
  },
40
+ replSet?: string,
45
41
  };
46
42
 
47
- async function generateDbName(dbName?: string): Promise<string> {
48
- return dbName || uuid();
49
- }
50
-
51
43
  async function generateConnectionString(port: number, dbName: string): Promise<string> {
52
44
  return `mongodb://127.0.0.1:${port}/${dbName}`;
53
45
  }
@@ -56,7 +48,7 @@ export default class MongoMemoryServer {
56
48
  isRunning: boolean = false;
57
49
  runningInstance: ?Promise<MongoInstanceDataT>;
58
50
  opts: MongoMemoryServerOptsT;
59
- debug: Function;
51
+ debug: DebugFn;
60
52
 
61
53
  constructor(opts?: $Shape<MongoMemoryServerOptsT> = {}) {
62
54
  this.opts = opts;
@@ -113,9 +105,10 @@ export default class MongoMemoryServer {
113
105
  data.port = await getport({ port: instOpts.port });
114
106
  this.debug = Debug(`Mongo[${data.port}]`);
115
107
  this.debug.enabled = !!this.opts.debug;
116
- data.dbName = await generateDbName(instOpts.dbName);
108
+ data.dbName = generateDbName(instOpts.dbName);
117
109
  data.uri = await generateConnectionString(data.port, data.dbName);
118
110
  data.storageEngine = instOpts.storageEngine || 'ephemeralForTest';
111
+ data.replSet = instOpts.replSet;
119
112
  if (instOpts.dbPath) {
120
113
  data.dbPath = instOpts.dbPath;
121
114
  } else {
@@ -133,6 +126,9 @@ export default class MongoMemoryServer {
133
126
  storageEngine: data.storageEngine,
134
127
  dbPath: data.dbPath,
135
128
  debug: this.opts.instance.debug,
129
+ replSet: data.replSet,
130
+ args: this.opts.instance.args,
131
+ auth: this.opts.instance.auth,
136
132
  },
137
133
  binary: this.opts.binary,
138
134
  spawn: this.opts.spawn,
@@ -179,7 +175,7 @@ export default class MongoMemoryServer {
179
175
  return generateConnectionString(port, otherDbName);
180
176
  }
181
177
  // generate new random db name
182
- return generateConnectionString(port, await generateDbName());
178
+ return generateConnectionString(port, generateDbName());
183
179
  }
184
180
 
185
181
  return uri;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+
3
+ var _mongodb = require("mongodb");
4
+
5
+ var _MongoMemoryServer = _interopRequireDefault(require("../MongoMemoryServer"));
6
+
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+
9
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
10
+
11
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
12
+
13
+ jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;
14
+ let con1;
15
+ let con2;
16
+ let db1;
17
+ let db2;
18
+ let mongoServer1;
19
+ let mongoServer2;
20
+ beforeAll(
21
+ /*#__PURE__*/
22
+ _asyncToGenerator(function* () {
23
+ mongoServer1 = new _MongoMemoryServer.default();
24
+ const mongoUri = yield mongoServer1.getConnectionString();
25
+ con1 = yield _mongodb.MongoClient.connect(mongoUri);
26
+ db1 = con1.db((yield mongoServer1.getDbName()));
27
+ mongoServer2 = new _MongoMemoryServer.default();
28
+ const mongoUri2 = yield mongoServer2.getConnectionString();
29
+ con2 = yield _mongodb.MongoClient.connect(mongoUri2);
30
+ db2 = con2.db((yield mongoServer1.getDbName()));
31
+ }));
32
+ afterAll(() => {
33
+ con1.close();
34
+ con2.close();
35
+ mongoServer1.stop();
36
+ mongoServer2.stop();
37
+ });
38
+ describe('Multiple mongoServers', () => {
39
+ it('should start several servers',
40
+ /*#__PURE__*/
41
+ _asyncToGenerator(function* () {
42
+ expect(db1).toBeDefined();
43
+ const col1 = db1.collection('test');
44
+ const result1 = yield col1.insert([{
45
+ a: 1
46
+ }, {
47
+ b: 1
48
+ }]);
49
+ expect(result1.result).toMatchSnapshot();
50
+ expect((yield col1.count({}))).toBe(2);
51
+ expect(db2).toBeDefined();
52
+ const col2 = db2.collection('test');
53
+ const result2 = yield col2.insert([{
54
+ a: 2
55
+ }, {
56
+ b: 2
57
+ }]);
58
+ expect(result2.result).toMatchSnapshot();
59
+ expect((yield col2.count({}))).toBe(2);
60
+ }));
61
+ it('should have different uri',
62
+ /*#__PURE__*/
63
+ _asyncToGenerator(function* () {
64
+ const uri1 = yield mongoServer1.getConnectionString();
65
+ const uri2 = yield mongoServer2.getConnectionString();
66
+ expect(uri1).not.toEqual(uri2);
67
+ }));
68
+ });
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+
3
+ var _MongoMemoryReplSet = _interopRequireDefault(require("../MongoMemoryReplSet"));
4
+
5
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
+
7
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
8
+
9
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
10
+
11
+ describe('single server replset', () => {
12
+ let replSet;
13
+ afterEach(
14
+ /*#__PURE__*/
15
+ _asyncToGenerator(function* () {
16
+ if (!(replSet && replSet._state === 'running')) return;
17
+ yield replSet.stop();
18
+ replSet = null;
19
+ }));
20
+ it('should enter running state',
21
+ /*#__PURE__*/
22
+ _asyncToGenerator(function* () {
23
+ replSet = new _MongoMemoryReplSet.default();
24
+ yield replSet.waitUntilRunning();
25
+ const uri = yield replSet.getUri();
26
+ expect(uri.split(',').length).toEqual(1);
27
+ }));
28
+ it('should be able to get connection string to specific db',
29
+ /*#__PURE__*/
30
+ _asyncToGenerator(function* () {
31
+ replSet = new _MongoMemoryReplSet.default();
32
+ yield replSet.waitUntilRunning();
33
+ const uri = yield replSet.getUri('other');
34
+ const str = yield replSet.getConnectionString('other');
35
+ expect(uri.split(',').length).toEqual(1);
36
+ expect(uri.endsWith('/other')).toBeTruthy();
37
+ expect(str).toEqual(uri);
38
+ }));
39
+ it('should be able to get dbName',
40
+ /*#__PURE__*/
41
+ _asyncToGenerator(function* () {
42
+ const opts = {
43
+ autoStart: false,
44
+ replSet: {
45
+ dbName: 'static'
46
+ }
47
+ };
48
+ replSet = new _MongoMemoryReplSet.default(opts);
49
+ const dbName = yield replSet.getDbName();
50
+ expect(dbName).toEqual('static');
51
+ }));
52
+ it('should not autostart if autostart: false',
53
+ /*#__PURE__*/
54
+ _asyncToGenerator(function* () {
55
+ replSet = new _MongoMemoryReplSet.default({
56
+ autoStart: false
57
+ });
58
+ yield new Promise((resolve, reject) => {
59
+ replSet.once('state', state => reject(new Error(`Invalid state: ${state}`)));
60
+ setTimeout(resolve, 500);
61
+ });
62
+ }));
63
+ });
64
+ describe('multi-member replica set', () => {
65
+ let replSet;
66
+ afterEach(
67
+ /*#__PURE__*/
68
+ _asyncToGenerator(function* () {
69
+ if (!replSet) return;
70
+ yield replSet.stop();
71
+ replSet = null;
72
+ }));
73
+ it('should enter running state',
74
+ /*#__PURE__*/
75
+ _asyncToGenerator(function* () {
76
+ const opts = {
77
+ replSet: {
78
+ count: 3
79
+ }
80
+ };
81
+ replSet = new _MongoMemoryReplSet.default(opts);
82
+ yield replSet.waitUntilRunning();
83
+ expect(replSet.servers.length).toEqual(3);
84
+ const uri = yield replSet.getUri();
85
+ expect(uri.split(',').length).toEqual(3);
86
+ }), 40000);
87
+ });
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+
3
+ var _mongodb = require("mongodb");
4
+
5
+ var _MongoMemoryServer = _interopRequireDefault(require("../MongoMemoryServer"));
6
+
7
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+
9
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
10
+
11
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
12
+
13
+ jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;
14
+ let con;
15
+ let db;
16
+ let mongoServer;
17
+ beforeAll(
18
+ /*#__PURE__*/
19
+ _asyncToGenerator(function* () {
20
+ mongoServer = new _MongoMemoryServer.default();
21
+ const mongoUri = yield mongoServer.getConnectionString();
22
+ con = yield _mongodb.MongoClient.connect(mongoUri);
23
+ db = con.db((yield mongoServer.getDbName()));
24
+ }));
25
+ afterAll(() => {
26
+ con.close();
27
+ mongoServer.stop();
28
+ });
29
+ describe('Single mongoServer', () => {
30
+ it('should start mongo server',
31
+ /*#__PURE__*/
32
+ _asyncToGenerator(function* () {
33
+ expect(db).toBeDefined();
34
+ const col = db.collection('test');
35
+ const result = yield col.insert([{
36
+ a: 1
37
+ }, {
38
+ b: 1
39
+ }]);
40
+ expect(result.result).toMatchSnapshot();
41
+ expect((yield col.count({}))).toBe(2);
42
+ }));
43
+ });
package/lib/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import MongoBinary from './util/MongoBinary';
2
+ import MongoInstance from './util/MongoInstance';
3
+ import MongoMemoryServer from './MongoMemoryServer';
4
+
5
+ export default MongoMemoryServer;
6
+ export { MongoBinary, MongoInstance, MongoMemoryServer };