Skip to content

Commit 5682192

Browse files
authored
chore(cli-repl): use strict TS flag for test files (#2149)
1 parent 2c58118 commit 5682192

File tree

8 files changed

+71
-54
lines changed

8 files changed

+71
-54
lines changed

packages/cli-repl/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"compile": "tsc -p tsconfig.json",
2222
"start": "node bin/mongosh.js",
2323
"pretest": "npm run compile",
24-
"test": "cross-env TS_NODE_PROJECT=../../configs/tsconfig-mongosh/tsconfig.test.json mocha -r \"../../scripts/import-expansions.js\" --timeout 60000 -r ts-node/register \"./src/**/*.spec.ts\"",
24+
"test": "mocha -r \"../../scripts/import-expansions.js\" --timeout 60000 -r ts-node/register \"./src/**/*.spec.ts\"",
2525
"test-ci": "node ../../scripts/run-if-package-requested.js npm test",
2626
"test-coverage": "nyc --no-clean --cwd ../.. --reporter=none npm run test",
2727
"test-ci-coverage": "nyc --no-clean --cwd ../.. --reporter=none npm run test-ci",

packages/cli-repl/src/arg-parser.spec.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -705,8 +705,8 @@ describe('arg-parser', function () {
705705
});
706706

707707
it('sets the filenames', function () {
708-
expect(parseCliArgs(argv).fileNames[0]).to.equal('test1.js');
709-
expect(parseCliArgs(argv).fileNames[1]).to.equal('test2.js');
708+
expect(parseCliArgs(argv).fileNames?.[0]).to.equal('test1.js');
709+
expect(parseCliArgs(argv).fileNames?.[1]).to.equal('test2.js');
710710
});
711711
});
712712

@@ -718,8 +718,8 @@ describe('arg-parser', function () {
718718
});
719719

720720
it('sets the filenames', function () {
721-
expect(parseCliArgs(argv).fileNames[0]).to.equal('test1.mongodb');
722-
expect(parseCliArgs(argv).fileNames[1]).to.equal('test2.mongodb');
721+
expect(parseCliArgs(argv).fileNames?.[0]).to.equal('test1.mongodb');
722+
expect(parseCliArgs(argv).fileNames?.[1]).to.equal('test2.mongodb');
723723
});
724724
});
725725

@@ -731,8 +731,8 @@ describe('arg-parser', function () {
731731
});
732732

733733
it('sets the filenames', function () {
734-
expect(parseCliArgs(argv).fileNames[0]).to.equal('test1.txt');
735-
expect(parseCliArgs(argv).fileNames[1]).to.equal('test2.txt');
734+
expect(parseCliArgs(argv).fileNames?.[0]).to.equal('test1.txt');
735+
expect(parseCliArgs(argv).fileNames?.[1]).to.equal('test2.txt');
736736
});
737737
});
738738

@@ -744,8 +744,8 @@ describe('arg-parser', function () {
744744
});
745745

746746
it('sets the filenames', function () {
747-
expect(parseCliArgs(argv).fileNames[0]).to.equal('test1.txt');
748-
expect(parseCliArgs(argv).fileNames[1]).to.equal('test2.txt');
747+
expect(parseCliArgs(argv).fileNames?.[0]).to.equal('test1.txt');
748+
expect(parseCliArgs(argv).fileNames?.[1]).to.equal('test2.txt');
749749
});
750750
});
751751

@@ -764,8 +764,8 @@ describe('arg-parser', function () {
764764
});
765765

766766
it('sets the filenames', function () {
767-
expect(parseCliArgs(argv).fileNames[0]).to.equal('test1.txt');
768-
expect(parseCliArgs(argv).fileNames[1]).to.equal('test2.txt');
767+
expect(parseCliArgs(argv).fileNames?.[0]).to.equal('test1.txt');
768+
expect(parseCliArgs(argv).fileNames?.[1]).to.equal('test2.txt');
769769
});
770770
});
771771
});
@@ -779,8 +779,8 @@ describe('arg-parser', function () {
779779
});
780780

781781
it('sets the filenames', function () {
782-
expect(parseCliArgs(argv).fileNames[0]).to.equal('test1.js');
783-
expect(parseCliArgs(argv).fileNames[1]).to.equal('test2.js');
782+
expect(parseCliArgs(argv).fileNames?.[0]).to.equal('test1.js');
783+
expect(parseCliArgs(argv).fileNames?.[1]).to.equal('test2.js');
784784
});
785785
});
786786

@@ -792,8 +792,8 @@ describe('arg-parser', function () {
792792
});
793793

794794
it('sets the filenames', function () {
795-
expect(parseCliArgs(argv).fileNames[0]).to.equal('test1.mongodb');
796-
expect(parseCliArgs(argv).fileNames[1]).to.equal('test2.mongodb');
795+
expect(parseCliArgs(argv).fileNames?.[0]).to.equal('test1.mongodb');
796+
expect(parseCliArgs(argv).fileNames?.[1]).to.equal('test2.mongodb');
797797
});
798798
});
799799

@@ -807,7 +807,7 @@ describe('arg-parser', function () {
807807
});
808808

809809
it('uses the remainder as filenames', function () {
810-
expect(parseCliArgs(argv).fileNames[0]).to.equal('test2.txt');
810+
expect(parseCliArgs(argv).fileNames?.[0]).to.equal('test2.txt');
811811
});
812812
});
813813

@@ -821,7 +821,7 @@ describe('arg-parser', function () {
821821
});
822822

823823
it('uses the remainder as filenames', function () {
824-
expect(parseCliArgs(argv).fileNames[0]).to.equal('test2.txt');
824+
expect(parseCliArgs(argv).fileNames?.[0]).to.equal('test2.txt');
825825
});
826826
});
827827

@@ -842,7 +842,7 @@ describe('arg-parser', function () {
842842
});
843843

844844
it('uses the remainder as filenames', function () {
845-
expect(parseCliArgs(argv).fileNames[0]).to.equal(
845+
expect(parseCliArgs(argv).fileNames?.[0]).to.equal(
846846
'mongodb://domain.foo.js'
847847
);
848848
});
@@ -892,7 +892,7 @@ describe('arg-parser', function () {
892892
});
893893

894894
context('when providing a deprecated argument', function () {
895-
[
895+
for (const { deprecated, replacement, value } of [
896896
{ deprecated: 'ssl', replacement: 'tls' },
897897
{
898898
deprecated: 'sslAllowInvalidCertificates',
@@ -929,7 +929,7 @@ describe('arg-parser', function () {
929929
replacement: 'tlsDisabledProtocols',
930930
value: 'disabledProtos',
931931
},
932-
].forEach(({ deprecated, replacement, value }) => {
932+
] as const) {
933933
it(`replaces --${deprecated} with --${replacement}`, function () {
934934
const argv = [...baseArgv, `--${deprecated}`];
935935
if (value) {
@@ -940,7 +940,7 @@ describe('arg-parser', function () {
940940
expect(args).to.not.have.property(deprecated);
941941
expect(args[replacement]).to.equal(value ?? true);
942942
});
943-
});
943+
}
944944
});
945945
});
946946
});

packages/cli-repl/src/async-repl.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ describe('AsyncRepl', function () {
173173
exited = true;
174174
});
175175

176-
let resolve;
176+
let resolve!: () => void;
177177
repl.context.asyncFn = () =>
178-
new Promise((res) => {
178+
new Promise<void>((res) => {
179179
resolve = res;
180180
});
181181

packages/cli-repl/src/cli-repl.spec.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ describe('CliRepl', function () {
5151
const tmpdir = useTmpdir();
5252

5353
async function log(): Promise<any[]> {
54+
if (!cliRepl.logWriter?.logFilePath) return [];
5455
await cliRepl.logWriter.flush(); // Ensure any pending data is written first
5556
return readReplLogfile(cliRepl.logWriter.logFilePath);
5657
}
@@ -76,8 +77,8 @@ describe('CliRepl', function () {
7677
});
7778
exitCode = null;
7879

79-
let resolveExitPromise;
80-
exitPromise = new Promise((resolve) => {
80+
let resolveExitPromise!: () => void;
81+
exitPromise = new Promise<void>((resolve) => {
8182
resolveExitPromise = resolve;
8283
});
8384

@@ -858,7 +859,7 @@ describe('CliRepl', function () {
858859
try {
859860
await cliRepl.start('', {});
860861
expect.fail('missed exception');
861-
} catch (err) {
862+
} catch (err: any) {
862863
expect(err.message).to.equal('nested error');
863864
}
864865
});
@@ -869,7 +870,7 @@ describe('CliRepl', function () {
869870
try {
870871
await cliRepl.start('', {});
871872
expect.fail('missed exception');
872-
} catch (err) {
873+
} catch (err: any) {
873874
expect(err.message).to.equal(
874875
'Cannot use --json without --eval or with --shell or with extra files'
875876
);
@@ -884,7 +885,7 @@ describe('CliRepl', function () {
884885
try {
885886
await cliRepl.start('', {});
886887
expect.fail('missed exception');
887-
} catch (err) {
888+
} catch (err: any) {
888889
expect(err.message).to.equal(
889890
'Cannot use --json without --eval or with --shell or with extra files'
890891
);
@@ -899,7 +900,7 @@ describe('CliRepl', function () {
899900
try {
900901
await cliRepl.start('', {});
901902
expect.fail('missed exception');
902-
} catch (err) {
903+
} catch (err: any) {
903904
expect(err.message).to.equal(
904905
'Cannot use --json without --eval or with --shell or with extra files'
905906
);
@@ -1285,7 +1286,7 @@ describe('CliRepl', function () {
12851286
it('does not emit warnings when connecting multiple times', async function () {
12861287
await cliRepl.start(await testServer.connectionString(), {});
12871288
let warnings = 0;
1288-
const warningListener = (warning) => {
1289+
const warningListener = (warning: unknown) => {
12891290
console.log('Unexpected warning', warning);
12901291
warnings++;
12911292
};
@@ -1407,7 +1408,9 @@ describe('CliRepl', function () {
14071408
input.write('exit\n');
14081409
await waitBus(cliRepl.bus, 'mongosh:closed');
14091410
const useEvents = requests.flatMap((req) =>
1410-
JSON.parse(req.body).batch.filter((entry) => entry.event === 'Use')
1411+
JSON.parse(req.body).batch.filter(
1412+
(entry: any) => entry.event === 'Use'
1413+
)
14111414
);
14121415
expect(useEvents).to.have.lengthOf(1);
14131416
});
@@ -1425,7 +1428,9 @@ describe('CliRepl', function () {
14251428
input.write('exit\n');
14261429
await waitBus(cliRepl.bus, 'mongosh:closed');
14271430
const useEvents = requests.flatMap((req) =>
1428-
JSON.parse(req.body).batch.filter((entry) => entry.event === 'Use')
1431+
JSON.parse(req.body).batch.filter(
1432+
(entry: any) => entry.event === 'Use'
1433+
)
14291434
);
14301435
expect(useEvents).to.have.lengthOf(0);
14311436
});
@@ -1448,7 +1453,9 @@ describe('CliRepl', function () {
14481453
input.write('exit\n');
14491454
await waitBus(cliRepl.bus, 'mongosh:closed');
14501455
const useEvents = requests.flatMap((req) =>
1451-
JSON.parse(req.body).batch.filter((entry) => entry.event === 'Use')
1456+
JSON.parse(req.body).batch.filter(
1457+
(entry: any) => entry.event === 'Use'
1458+
)
14521459
);
14531460
expect(useEvents).to.have.lengthOf(2);
14541461
});
@@ -1469,7 +1476,7 @@ describe('CliRepl', function () {
14691476
const loadEvents = requests
14701477
.map((req) =>
14711478
JSON.parse(req.body).batch.filter(
1472-
(entry) => entry.event === 'Script Loaded'
1479+
(entry: any) => entry.event === 'Script Loaded'
14731480
)
14741481
)
14751482
.flat();
@@ -1486,7 +1493,7 @@ describe('CliRepl', function () {
14861493
const apiEvents = requests
14871494
.map((req) =>
14881495
JSON.parse(req.body).batch.filter(
1489-
(entry) => entry.event === 'API Call'
1496+
(entry: any) => entry.event === 'API Call'
14901497
)
14911498
)
14921499
.flat();
@@ -1500,12 +1507,12 @@ describe('CliRepl', function () {
15001507

15011508
it('includes a statement about flushed telemetry in the log', async function () {
15021509
await cliRepl.start(await testServer.connectionString(), {});
1503-
const { logFilePath } = cliRepl.logWriter;
1510+
const { logFilePath } = cliRepl.logWriter!;
15041511
input.write('db.hello()\n');
15051512
input.write('exit\n');
15061513
await waitBus(cliRepl.bus, 'mongosh:closed');
15071514
const flushEntry = (await readReplLogfile(logFilePath)).find(
1508-
(entry) => entry.id === 1_000_000_045
1515+
(entry: any) => entry.id === 1_000_000_045
15091516
);
15101517
expect(flushEntry.attr.flushError).to.equal(null);
15111518
expect(flushEntry.attr.flushDuration).to.be.a('number');
@@ -1522,7 +1529,7 @@ describe('CliRepl', function () {
15221529
expect(
15231530
requests
15241531
.flatMap((req) =>
1525-
JSON.parse(req.body).batch.map((entry) => entry.event)
1532+
JSON.parse(req.body).batch.map((entry: any) => entry.event)
15261533
)
15271534
.sort()
15281535
.filter(Boolean)
@@ -1550,7 +1557,7 @@ describe('CliRepl', function () {
15501557
const apiEvents = requests
15511558
.map((req) =>
15521559
JSON.parse(req.body).batch.filter(
1553-
(entry) => entry.event === 'API Call'
1560+
(entry: any) => entry.event === 'API Call'
15541561
)
15551562
)
15561563
.flat();
@@ -1712,7 +1719,7 @@ describe('CliRepl', function () {
17121719

17131720
const connectEvents = requests.flatMap((req) =>
17141721
JSON.parse(req.body).batch.filter(
1715-
(entry) => entry.event === 'New Connection'
1722+
(entry: any) => entry.event === 'New Connection'
17161723
)
17171724
);
17181725
expect(connectEvents).to.have.lengthOf(1);

packages/cli-repl/src/format-output.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ test 558.79 GiB
510510
function () {
511511
for (const type of ['ExplainOutput', 'ExplainableCursor']) {
512512
it(`returns output with large depth (${type})`, function () {
513-
const value = {};
513+
const value: Record<string, unknown> = {};
514514
let it = value;
515515
for (let i = 0; i <= 20; i++) {
516516
it = it[`level${i}`] = {};

packages/cli-repl/src/mongosh-repl.spec.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ describe('MongoshNodeRepl', function () {
7171
return 'success';
7272
});
7373
cp.listConfigOptions.callsFake(() => Object.keys(config));
74-
cp.exit.callsFake(((code) => bus.emit('test-exit-event', code)) as any);
74+
cp.exit.callsFake(((code: number) =>
75+
bus.emit('test-exit-event', code)) as any);
7576

7677
ioProvider = cp;
7778

@@ -91,8 +92,8 @@ describe('MongoshNodeRepl', function () {
9192
serviceProvider = sp;
9293
calledServiceProviderFunctions = () =>
9394
Object.fromEntries(
94-
Object.keys(sp)
95-
.map((key) => [key, sp[key]?.callCount])
95+
Object.entries(sp)
96+
.map(([key, value]) => [key, value?.callCount])
9697
.filter(([, count]) => !!count)
9798
);
9899

@@ -105,7 +106,7 @@ describe('MongoshNodeRepl', function () {
105106
mongoshRepl = new MongoshNodeRepl(mongoshReplOptions);
106107
});
107108

108-
let originalEnvVars;
109+
let originalEnvVars: Record<string, string | undefined>;
109110
before(function () {
110111
originalEnvVars = { ...process.env };
111112
});
@@ -527,7 +528,9 @@ describe('MongoshNodeRepl', function () {
527528
let getHistory: () => string[];
528529

529530
beforeEach(function () {
530-
const { history } = mongoshRepl.runtimeState().repl as any;
531+
const { history } = mongoshRepl.runtimeState().repl as unknown as {
532+
history: string[];
533+
};
531534
getHistory = () =>
532535
history.filter((line) => !line.startsWith('prefill-'));
533536
for (let i = 0; i < prefill; i++) {
@@ -827,10 +830,12 @@ describe('MongoshNodeRepl', function () {
827830
});
828831

829832
it('does not refresh the prompt if a window resize occurs while evaluating', async function () {
830-
let resolveInProgress;
831-
mongoshRepl.runtimeState().context.inProgress = new Promise((resolve) => {
832-
resolveInProgress = resolve;
833-
});
833+
let resolveInProgress!: () => void;
834+
mongoshRepl.runtimeState().context.inProgress = new Promise<void>(
835+
(resolve) => {
836+
resolveInProgress = resolve;
837+
}
838+
);
834839
input.write('inProgress\n');
835840
await tick();
836841

@@ -933,7 +938,9 @@ describe('MongoshNodeRepl', function () {
933938
context('user prompts', function () {
934939
beforeEach(function () {
935940
// No boolean equivalent for 'passwordPrompt' in the API, so provide one:
936-
mongoshRepl.runtimeState().context.booleanPrompt = (question) => {
941+
mongoshRepl.runtimeState().context.booleanPrompt = (
942+
question: string
943+
) => {
937944
return Object.assign(mongoshRepl.onPrompt(question, 'yesno'), {
938945
[Symbol.for('@@mongosh.syntheticPromise')]: true,
939946
});

packages/cli-repl/src/tls-certificate-selector.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('arg-mapper.applyTlsCertificateSelector', function () {
1616
);
1717
exportCertificateAndPrivateKey = sinon.stub();
1818
require(process.env.TEST_OS_EXPORT_CERTIFICATE_AND_KEY_PATH).setFn(
19-
(search) => exportCertificateAndPrivateKey(search)
19+
(search: unknown) => exportCertificateAndPrivateKey(search)
2020
);
2121
});
2222
afterEach(function () {

0 commit comments

Comments
 (0)