Skip to content

Commit

Permalink
feat(MongoMemoryReplSet): add named error for "count" assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
hasezoey committed Aug 31, 2021
1 parent 88ae810 commit d67202b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
7 changes: 7 additions & 0 deletions docs/guides/error-warning-details.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,10 @@ Example: `Assert failed - no custom error`

Details:
This Error gets thrown when no custom error to `assertion` is given, this should never happen

## ReplsetCountLowError

Example: `ReplSet Count needs to be 1 or higher! (specified count: "${count}")`

Details:
ReplSet count (like `new MongoMemoryReplSet({ replSet: { count: 0 } })`) needs to be `1` or higher
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { SpawnOptions } from 'child_process';
import {
AuthNotObjectError,
InstanceInfoError,
ReplsetCountLowError,
StateError,
WaitForPrimaryTimeoutError,
} from './util/errors';
Expand Down Expand Up @@ -251,7 +252,7 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
};
this._replSetOpts = { ...defaults, ...val };

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

if (typeof this._replSetOpts.auth === 'object') {
this._replSetOpts.auth = authDefault(this._replSetOpts.auth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { MongoClient } from 'mongodb';
import MongoMemoryServer from '../MongoMemoryServer';
import * as utils from '../util/utils';
import { MongoMemoryInstanceOpts } from '../util/MongoInstance';
import { StateError, WaitForPrimaryTimeoutError } from '../util/errors';
import { ReplsetCountLowError, StateError, WaitForPrimaryTimeoutError } from '../util/errors';
import { assertIsError } from './testUtils/test_utils';

jest.setTimeout(100000); // 10s
Expand Down Expand Up @@ -78,6 +78,8 @@ describe('single server replset', () => {
new MongoMemoryReplSet({ replSet: { count: 0 } });
fail('Expected "new MongoMemoryReplSet" to throw an error');
} catch (err) {
expect(err).toBeInstanceOf(ReplsetCountLowError);
expect(err).toHaveProperty('count', 0);
assertIsError(err);
expect(err.message).toMatchSnapshot();
}
Expand Down Expand Up @@ -422,6 +424,8 @@ describe('MongoMemoryReplSet', () => {
replSet.replSetOpts = { count: 0 };
fail('Expected assignment of "replSet.instanceOpts" to fail');
} catch (err) {
expect(err).toBeInstanceOf(ReplsetCountLowError);
expect(err).toHaveProperty('count', 0);
assertIsError(err);
expect(err.message).toMatchSnapshot();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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!"`;
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\\")"`;

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

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!"`;
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\\")"`;

exports[`single server replset "start" should throw an error if _state is not "stopped" 1`] = `
"Incorrect State for operation: \\"running\\", allowed States: \\"[stopped]\\"
Expand Down
6 changes: 6 additions & 0 deletions packages/mongodb-memory-server-core/src/util/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,9 @@ export class AssertionFallbackError extends Error {
super('Assert failed - no custom error');
}
}

export class ReplsetCountLowError extends Error {
constructor(public count: number) {
super(`ReplSet Count needs to be 1 or higher! (specified count: "${count}")`);
}
}

0 comments on commit d67202b

Please sign in to comment.