Skip to content

Commit 5970a71

Browse files
authored
chore(data-service): typecheck tests (#7549)
1 parent 1e8888f commit 5970a71

File tree

8 files changed

+45
-61
lines changed

8 files changed

+45
-61
lines changed

packages/data-service/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"test-watch": "npm run test -- --watch",
5050
"test-ci": "npm run test-cov -- -- --include \"./**/*.{spec,test}.*\" --exclude \"./src/connect.spec.ts\" --exclude \"./src/csfle-collection-tracker.spec.ts\"",
5151
"reformat": "npm run eslint . -- --fix && npm run prettier -- --write .",
52-
"typecheck": "echo \"TODO(COMPASS-9897): typecheck is failing in test files\" && tsc -p tsconfig-build.json --noEmit"
52+
"typecheck": "tsc -p tsconfig.json --noEmit"
5353
},
5454
"dependencies": {
5555
"@mongodb-js/compass-logging": "^1.7.23",

packages/data-service/src/connect.spec.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { UUID } from 'mongodb';
1111

1212
import connect from './connect';
1313
import type { ConnectionOptions } from './connection-options';
14-
import type DataService from './data-service';
14+
import type { DataServiceImpl, default as DataService } from './data-service';
1515
import { redactConnectionOptions } from './redact';
1616
import { runCommand } from './run-command';
1717
import { MongoLogWriter } from 'mongodb-log-writer';
@@ -37,7 +37,7 @@ const buildConnectionString = (
3737
username: string | undefined,
3838
password: string | undefined,
3939
host: string | undefined,
40-
params?: MongoClientOptions
40+
params?: Partial<Record<keyof MongoClientOptions, string>>
4141
): string => {
4242
if (!username || !password || !host) {
4343
return '';
@@ -135,7 +135,7 @@ describe('connect', function () {
135135
return this.skip();
136136
}
137137

138-
let dataService: DataService;
138+
let dataService: DataService | undefined;
139139

140140
try {
141141
dataService = await connect({
@@ -147,11 +147,14 @@ describe('connect', function () {
147147
const explainPlan = await dataService.explainFind('test.test', {}, {});
148148

149149
const targetHost = explainPlan?.serverInfo?.host;
150+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
150151
const replSetStatus: any = await runCommand(
151-
dataService['_database']('admin', 'META'),
152+
(dataService as DataServiceImpl)['_database']('admin', 'META'),
153+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
152154
{ replSetGetStatus: 1 } as any
153155
);
154-
const targetHostStatus = replSetStatus?.members.find((member) =>
156+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
157+
const targetHostStatus = replSetStatus?.members.find((member: any) =>
155158
member.name.startsWith(targetHost)
156159
);
157160

@@ -166,7 +169,7 @@ describe('connect', function () {
166169
return this.skip();
167170
}
168171

169-
let dataService: DataService;
172+
let dataService: DataService | undefined;
170173

171174
try {
172175
dataService = await connect({
@@ -177,18 +180,23 @@ describe('connect', function () {
177180

178181
const explainPlan = await dataService.explainFind('test.test', {}, {});
179182

183+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
180184
const replSetGetConfig: any = await runCommand(
181-
dataService['_database']('admin', 'META'),
185+
(dataService as DataServiceImpl)['_database']('admin', 'META'),
186+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
182187
{ replSetGetConfig: 1 } as any
183188
);
184189

185-
const analtyticsNode = replSetGetConfig?.config?.members.find(
186-
(member) => member?.tags.nodeType === 'ANALYTICS'
190+
const analyticsNode = replSetGetConfig?.config?.members.find(
191+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
192+
(member: any) => member?.tags.nodeType === 'ANALYTICS'
187193
);
188194

195+
expect(explainPlan?.serverInfo?.host).to.exist;
196+
189197
// test that queries are routed to the analytics node
190198
expect(explainPlan?.serverInfo?.host).to.be.equal(
191-
analtyticsNode?.host.split(':')[0]
199+
analyticsNode?.host.split(':')[0]
192200
);
193201
} finally {
194202
await dataService?.disconnect();
@@ -694,7 +702,7 @@ async function connectAndGetAuthInfo(connectionOptions: ConnectionOptions) {
694702
: undefined,
695703
});
696704
const connectionStatus = await runCommand(
697-
dataService['_database']('admin', 'META'),
705+
(dataService as DataServiceImpl)['_database']('admin', 'META'),
698706
{ connectionStatus: 1 }
699707
);
700708

packages/data-service/src/connection-attempt.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { mongoLogId } = createLogger('CONNECTION-ATTEMPT-TEST');
99

1010
describe('ConnectionAttempt Module', function () {
1111
let logger: UnboundDataServiceImplLogger;
12+
const proxyOptions = {};
1213

1314
beforeEach(function () {
1415
logger = {
@@ -30,6 +31,7 @@ describe('ConnectionAttempt Module', function () {
3031
return dataService;
3132
},
3233
logger,
34+
proxyOptions,
3335
});
3436
const connectionAttemptResult = await connectionAttempt.connect({
3537
connectionString: 'mongodb://localhost:27017',
@@ -45,6 +47,7 @@ describe('ConnectionAttempt Module', function () {
4547
return dataService;
4648
},
4749
logger,
50+
proxyOptions,
4851
});
4952

5053
const connectPromise = connectionAttempt.connect({
@@ -65,6 +68,7 @@ describe('ConnectionAttempt Module', function () {
6568
throw new Error('should have been thrown');
6669
},
6770
logger,
71+
proxyOptions,
6872
});
6973

7074
await connectionAttempt.connect({
@@ -93,6 +97,7 @@ describe('ConnectionAttempt Module', function () {
9397
return dataService;
9498
},
9599
logger,
100+
proxyOptions,
96101
});
97102
await connectionAttempt.connect({
98103
connectionString: 'mongodb://localhost:27017',

packages/data-service/src/csfle-collection-tracker.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ describe('CSFLECollectionTracker', function () {
371371
});
372372

373373
context('with server-side FLE2 schema info', function () {
374-
let SOME_UUID1, SOME_UUID2;
374+
let SOME_UUID1: Binary, SOME_UUID2: Binary;
375375
beforeEach(async function () {
376376
[tracker, dataService, SOME_UUID1, SOME_UUID2] = await createTracker();
377377
// Creating the collections needs to be done through the non-FLE-aware
@@ -468,7 +468,7 @@ describe('CSFLECollectionTracker', function () {
468468
err = error;
469469
}
470470
expect(err).to.be.instanceOf(Error);
471-
expect(err.message).to.match(
471+
expect((err as Error).message).to.match(
472472
/\[Compass\] Missing encrypted field information for collection/
473473
);
474474
});
@@ -493,7 +493,7 @@ describe('CSFLECollectionTracker', function () {
493493
err = error;
494494
}
495495
expect(err).to.be.instanceOf(Error);
496-
expect(err.message).to.match(
496+
expect((err as Error).message).to.match(
497497
/\[Compass\] Missing encrypted field 'a' of collection/
498498
);
499499
});

packages/data-service/src/data-service.spec.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import type {
1616
import EventEmitter from 'events';
1717
import type { ClientMockOptions } from '../test/helpers';
1818
import { createMongoClientMock } from '../test/helpers';
19-
import { AbortController } from '../test/mocks';
2019
import { createClonedClient } from './connect-mongo-client';
2120
import { runCommand } from './run-command';
2221
import { mochaTestServer } from '@mongodb-js/compass-test-server';
@@ -153,12 +152,14 @@ describe('DataService', function () {
153152
const { info: [, , , , startedAttr] = [] } = startedLog ?? {};
154153
expect(startedAttr).to.have.property(
155154
'connectionId',
156-
dataServiceLogTest._id
155+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
156+
(dataServiceLogTest as any)._id
157157
);
158158
const { info: [, , , , succeededAttr] = [] } = succeededLog ?? {};
159159
expect(succeededAttr).to.have.property(
160160
'connectionId',
161-
dataServiceLogTest._id
161+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
162+
(dataServiceLogTest as any)._id
162163
);
163164
});
164165

@@ -197,12 +198,14 @@ describe('DataService', function () {
197198
const { info: [, , , , startedAttr] = [] } = startedLog ?? {};
198199
expect(startedAttr).to.have.property(
199200
'connectionId',
200-
dataServiceLogTest._id
201+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
202+
(dataServiceLogTest as any)._id
201203
);
202204
const { info: [, , , , succeededAttr] = [] } = failedLog ?? {};
203205
expect(succeededAttr).to.have.property(
204206
'connectionId',
205-
dataServiceLogTest._id
207+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
208+
(dataServiceLogTest as any)._id
206209
);
207210
});
208211
});
@@ -269,17 +272,17 @@ describe('DataService', function () {
269272
connectionId,
270273
duration: 400,
271274
failure: new Error('fail'),
272-
});
275+
} as any);
273276
client.emit('serverHeartbeatFailed', {
274277
connectionId,
275278
duration: 600,
276279
failure: new Error('fail'),
277-
});
280+
} as any);
278281
client.emit('serverHeartbeatFailed', {
279282
connectionId,
280283
duration: 800,
281284
failure: new Error('fail'),
282-
});
285+
} as any);
283286
const logEntries = [
284287
// Picking the attrs part of the log
285288
...logger.debug.args.map((args) => args[4]),
@@ -1431,7 +1434,7 @@ describe('DataService', function () {
14311434
const topology = dataService.getLastSeenTopology();
14321435

14331436
expect(topology).to.not.be.null;
1434-
expect(topology!.servers.values().next().value.address).to.be.a(
1437+
expect(topology!.servers.values().next().value!.address).to.be.a(
14351438
'string'
14361439
);
14371440

packages/data-service/src/data-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1741,7 +1741,7 @@ class DataServiceImpl extends WithLogContext implements DataService {
17411741
filter: Filter<Document>,
17421742
options: CountDocumentsOptions = {},
17431743
executionOptions?: ExecutionOptions & {
1744-
fallbackReadPreference: ReadPreferenceMode;
1744+
fallbackReadPreference?: ReadPreferenceMode;
17451745
}
17461746
): Promise<number> {
17471747
return this._cancellableOperation(

packages/data-service/test/helpers.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ export function createMongoClientMock({
4040
} {
4141
const db = {
4242
command(spec: Document) {
43-
const cmd = Object.keys(spec).find((key) =>
44-
ALLOWED_COMMANDS.includes(key as (typeof ALLOWED_COMMANDS)[number])
43+
const cmd = Object.keys(spec).find(
44+
(key): key is (typeof ALLOWED_COMMANDS)[number] =>
45+
ALLOWED_COMMANDS.includes(key as (typeof ALLOWED_COMMANDS)[number])
4546
);
4647
if (cmd && commands[cmd]) {
4748
const command = commands[cmd];
@@ -55,7 +56,7 @@ export function createMongoClientMock({
5556
return Promise.reject(
5657
new Error(
5758
`not authorized on ${String(
58-
this.databaseName
59+
(this as unknown as ReturnType<MongoClient['db']>).databaseName
5960
)} to execute command ${JSON.stringify(spec)}`
6061
)
6162
);

packages/data-service/test/mocks.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)