Skip to content

Commit

Permalink
Refactor tests to align with #3144
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Feb 22, 2022
1 parent 83cd183 commit f934f64
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export {
MongoServerError,
MongoServerSelectionError,
MongoSystemError,
MongoTailableCursorError,
MongoTopologyClosedError,
MongoTransactionError,
MongoWriteConcernError
Expand Down
59 changes: 46 additions & 13 deletions test/unit/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,53 @@ import {
MongoSystemError,
NODE_IS_RECOVERING_ERROR_MESSAGE
} from '../../src/error';
import * as importsFromErrorSrc from '../../src/error';
import {
MongoError,
MongoNetworkError,
MongoParseError,
MongoServerError,
MongoWriteConcernError
MongoWriteConcernError,
TopologyDescription
} from '../../src/index';
import * as EverythingFromDriver from '../../src/index';
import * as importsFromEntryPoint from '../../src/index';
import { Topology } from '../../src/sdam/topology';
import { isHello, ns } from '../../src/utils';
import { isHello, ns, setDifference } from '../../src/utils';
import { ReplSetFixture } from '../tools/common';
import { cleanup } from '../tools/mongodb-mock/index';
import { getSymbolFrom } from '../tools/utils';

describe('MongoErrors', () => {
let errorClasses = Object.fromEntries(
Object.entries(EverythingFromDriver).filter(([key]) => key.endsWith('Error'))
let errorClassesFromEntryPoint = Object.fromEntries(
Object.entries(importsFromEntryPoint).filter(
([key, value]) => key.endsWith('Error') && value.toString().startsWith('class')
)
) as any;
errorClasses = { ...errorClasses, MongoPoolClosedError, MongoWaitQueueTimeoutError };
errorClassesFromEntryPoint = {
...errorClassesFromEntryPoint,
MongoPoolClosedError,
MongoWaitQueueTimeoutError
};

const errorClassesFromErrorSrc = Object.fromEntries(
Object.entries(importsFromErrorSrc).filter(
([key, value]) => key.endsWith('Error') && value.toString().startsWith('class')
)
);

it('all defined errors should be public', () => {
expect(
setDifference(Object.keys(errorClassesFromEntryPoint), Object.keys(errorClassesFromErrorSrc))
).to.have.property('size', 3);

expect(
setDifference(Object.keys(errorClassesFromErrorSrc), Object.keys(errorClassesFromEntryPoint))
).to.have.property('size', 0);
});

for (const [errorName, errorClass] of Object.entries(errorClasses)) {
describe(errorName, () => {
it(`name should be read-only`, () => {
describe('error names should be read-only', () => {
for (const [errorName, errorClass] of Object.entries(errorClassesFromEntryPoint)) {
it(`${errorName} should be read-only`, () => {
// Dynamically create error class with message
const error = new (errorClass as any)('generated by test', {});
// expect name property to be class name
Expand All @@ -48,8 +72,8 @@ describe('MongoErrors', () => {
} catch (err) {}
expect(error).to.have.property('name', errorName);
});
});
}
}
});

describe('MongoError#constructor', () => {
it('should accept a string', function () {
Expand Down Expand Up @@ -98,7 +122,7 @@ describe('MongoErrors', () => {
error: {
code: 123
}
} as any as EverythingFromDriver.TopologyDescription;
} as any as TopologyDescription;

const error = new MongoSystemError('something went wrong', topologyDescription);
expect(error).to.haveOwnProperty('code', 123);
Expand All @@ -107,7 +131,16 @@ describe('MongoErrors', () => {

context('when the topology description does not contain a code', () => {
it('contains the code as a top level property', () => {
const topologyDescription = {} as any as EverythingFromDriver.TopologyDescription;
const topologyDescription = { error: {} } as any as TopologyDescription;

const error = new MongoSystemError('something went wrong', topologyDescription);
expect(error).to.haveOwnProperty('code', undefined);
});
});

context('when the topology description does not contain an error property', () => {
it('contains the code as a top level property', () => {
const topologyDescription = {} as any as TopologyDescription;

const error = new MongoSystemError('something went wrong', topologyDescription);
expect(error).to.haveOwnProperty('code', undefined);
Expand Down

0 comments on commit f934f64

Please sign in to comment.