Skip to content

Commit d67202b

Browse files
committed
feat(MongoMemoryReplSet): add named error for "count" assertion
1 parent 88ae810 commit d67202b

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

docs/guides/error-warning-details.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,10 @@ Example: `Assert failed - no custom error`
8282

8383
Details:
8484
This Error gets thrown when no custom error to `assertion` is given, this should never happen
85+
86+
## ReplsetCountLowError
87+
88+
Example: `ReplSet Count needs to be 1 or higher! (specified count: "${count}")`
89+
90+
Details:
91+
ReplSet count (like `new MongoMemoryReplSet({ replSet: { count: 0 } })`) needs to be `1` or higher

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { SpawnOptions } from 'child_process';
2424
import {
2525
AuthNotObjectError,
2626
InstanceInfoError,
27+
ReplsetCountLowError,
2728
StateError,
2829
WaitForPrimaryTimeoutError,
2930
} from './util/errors';
@@ -251,7 +252,7 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
251252
};
252253
this._replSetOpts = { ...defaults, ...val };
253254

254-
assertion(this._replSetOpts.count > 0, new Error('ReplSet Count needs to be 1 or higher!'));
255+
assertion(this._replSetOpts.count > 0, new ReplsetCountLowError(this._replSetOpts.count));
255256

256257
if (typeof this._replSetOpts.auth === 'object') {
257258
this._replSetOpts.auth = authDefault(this._replSetOpts.auth);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { MongoClient } from 'mongodb';
77
import MongoMemoryServer from '../MongoMemoryServer';
88
import * as utils from '../util/utils';
99
import { MongoMemoryInstanceOpts } from '../util/MongoInstance';
10-
import { StateError, WaitForPrimaryTimeoutError } from '../util/errors';
10+
import { ReplsetCountLowError, StateError, WaitForPrimaryTimeoutError } from '../util/errors';
1111
import { assertIsError } from './testUtils/test_utils';
1212

1313
jest.setTimeout(100000); // 10s
@@ -78,6 +78,8 @@ describe('single server replset', () => {
7878
new MongoMemoryReplSet({ replSet: { count: 0 } });
7979
fail('Expected "new MongoMemoryReplSet" to throw an error');
8080
} catch (err) {
81+
expect(err).toBeInstanceOf(ReplsetCountLowError);
82+
expect(err).toHaveProperty('count', 0);
8183
assertIsError(err);
8284
expect(err.message).toMatchSnapshot();
8385
}
@@ -422,6 +424,8 @@ describe('MongoMemoryReplSet', () => {
422424
replSet.replSetOpts = { count: 0 };
423425
fail('Expected assignment of "replSet.instanceOpts" to fail');
424426
} catch (err) {
427+
expect(err).toBeInstanceOf(ReplsetCountLowError);
428+
expect(err).toHaveProperty('count', 0);
425429
assertIsError(err);
426430
expect(err.message).toMatchSnapshot();
427431
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`MongoMemoryReplSet "_waitForPrimary" should throw an error if timeout is reached 1`] = `"{\\"timeout\\":1}"`;
44

5-
exports[`MongoMemoryReplSet getters & setters setter of "replSetOpts" should throw an error if count is 1 or above 1`] = `"ReplSet Count needs to be 1 or higher!"`;
5+
exports[`MongoMemoryReplSet getters & setters setter of "replSetOpts" should throw an error if count is 1 or above 1`] = `"ReplSet Count needs to be 1 or higher! (specified count: \\"0\\")"`;
66

77
exports[`MongoMemoryReplSet getters & setters state errors setter of "binaryOpts" should throw an error if state is not "stopped" 1`] = `
88
"Incorrect State for operation: \\"init\\", allowed States: \\"[stopped]\\"
@@ -36,7 +36,7 @@ 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/migrate7#no-function-other-than-start-create-ensureinstance-will-be-starting-anything"
3737
`;
3838

39-
exports[`single server replset "new" should throw an error if replSet count is 0 or less 1`] = `"ReplSet Count needs to be 1 or higher!"`;
39+
exports[`single server replset "new" should throw an error if replSet count is 0 or less 1`] = `"ReplSet Count needs to be 1 or higher! (specified count: \\"0\\")"`;
4040

4141
exports[`single server replset "start" should throw an error if _state is not "stopped" 1`] = `
4242
"Incorrect State for operation: \\"running\\", allowed States: \\"[stopped]\\"

packages/mongodb-memory-server-core/src/util/errors.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,9 @@ export class AssertionFallbackError extends Error {
124124
super('Assert failed - no custom error');
125125
}
126126
}
127+
128+
export class ReplsetCountLowError extends Error {
129+
constructor(public count: number) {
130+
super(`ReplSet Count needs to be 1 or higher! (specified count: "${count}")`);
131+
}
132+
}

0 commit comments

Comments
 (0)