From f934f64d9c7cb7a6c1e6b34e711cb2240981676e Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Tue, 22 Feb 2022 14:40:38 -0500 Subject: [PATCH] Refactor tests to align with https://github.com/mongodb/node-mongodb-native/pull/3144 --- src/index.ts | 1 + test/unit/error.test.ts | 59 ++++++++++++++++++++++++++++++++--------- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/index.ts b/src/index.ts index 27c8ac944c..0502109519 100644 --- a/src/index.ts +++ b/src/index.ts @@ -62,6 +62,7 @@ export { MongoServerError, MongoServerSelectionError, MongoSystemError, + MongoTailableCursorError, MongoTopologyClosedError, MongoTransactionError, MongoWriteConcernError diff --git a/test/unit/error.test.ts b/test/unit/error.test.ts index ff3511db2e..2639dc260f 100644 --- a/test/unit/error.test.ts +++ b/test/unit/error.test.ts @@ -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 @@ -48,8 +72,8 @@ describe('MongoErrors', () => { } catch (err) {} expect(error).to.have.property('name', errorName); }); - }); - } + } + }); describe('MongoError#constructor', () => { it('should accept a string', function () { @@ -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); @@ -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);