From 5682192c843485fdc4076b3114a38315c5e576d3 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 4 Sep 2024 12:22:38 +0200 Subject: [PATCH] chore(cli-repl): use strict TS flag for test files (#2149) --- packages/cli-repl/package.json | 2 +- packages/cli-repl/src/arg-parser.spec.ts | 40 +++++++++--------- packages/cli-repl/src/async-repl.spec.ts | 4 +- packages/cli-repl/src/cli-repl.spec.ts | 41 +++++++++++-------- packages/cli-repl/src/format-output.spec.ts | 2 +- packages/cli-repl/src/mongosh-repl.spec.ts | 27 +++++++----- .../src/tls-certificate-selector.spec.ts | 2 +- packages/cli-repl/test/repl-helpers.ts | 7 +++- 8 files changed, 71 insertions(+), 54 deletions(-) diff --git a/packages/cli-repl/package.json b/packages/cli-repl/package.json index 11e0201c3..6b56f3e4b 100644 --- a/packages/cli-repl/package.json +++ b/packages/cli-repl/package.json @@ -21,7 +21,7 @@ "compile": "tsc -p tsconfig.json", "start": "node bin/mongosh.js", "pretest": "npm run compile", - "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\"", + "test": "mocha -r \"../../scripts/import-expansions.js\" --timeout 60000 -r ts-node/register \"./src/**/*.spec.ts\"", "test-ci": "node ../../scripts/run-if-package-requested.js npm test", "test-coverage": "nyc --no-clean --cwd ../.. --reporter=none npm run test", "test-ci-coverage": "nyc --no-clean --cwd ../.. --reporter=none npm run test-ci", diff --git a/packages/cli-repl/src/arg-parser.spec.ts b/packages/cli-repl/src/arg-parser.spec.ts index a69e257ba..ab09eff6a 100644 --- a/packages/cli-repl/src/arg-parser.spec.ts +++ b/packages/cli-repl/src/arg-parser.spec.ts @@ -705,8 +705,8 @@ describe('arg-parser', function () { }); it('sets the filenames', function () { - expect(parseCliArgs(argv).fileNames[0]).to.equal('test1.js'); - expect(parseCliArgs(argv).fileNames[1]).to.equal('test2.js'); + expect(parseCliArgs(argv).fileNames?.[0]).to.equal('test1.js'); + expect(parseCliArgs(argv).fileNames?.[1]).to.equal('test2.js'); }); }); @@ -718,8 +718,8 @@ describe('arg-parser', function () { }); it('sets the filenames', function () { - expect(parseCliArgs(argv).fileNames[0]).to.equal('test1.mongodb'); - expect(parseCliArgs(argv).fileNames[1]).to.equal('test2.mongodb'); + expect(parseCliArgs(argv).fileNames?.[0]).to.equal('test1.mongodb'); + expect(parseCliArgs(argv).fileNames?.[1]).to.equal('test2.mongodb'); }); }); @@ -731,8 +731,8 @@ describe('arg-parser', function () { }); it('sets the filenames', function () { - expect(parseCliArgs(argv).fileNames[0]).to.equal('test1.txt'); - expect(parseCliArgs(argv).fileNames[1]).to.equal('test2.txt'); + expect(parseCliArgs(argv).fileNames?.[0]).to.equal('test1.txt'); + expect(parseCliArgs(argv).fileNames?.[1]).to.equal('test2.txt'); }); }); @@ -744,8 +744,8 @@ describe('arg-parser', function () { }); it('sets the filenames', function () { - expect(parseCliArgs(argv).fileNames[0]).to.equal('test1.txt'); - expect(parseCliArgs(argv).fileNames[1]).to.equal('test2.txt'); + expect(parseCliArgs(argv).fileNames?.[0]).to.equal('test1.txt'); + expect(parseCliArgs(argv).fileNames?.[1]).to.equal('test2.txt'); }); }); @@ -764,8 +764,8 @@ describe('arg-parser', function () { }); it('sets the filenames', function () { - expect(parseCliArgs(argv).fileNames[0]).to.equal('test1.txt'); - expect(parseCliArgs(argv).fileNames[1]).to.equal('test2.txt'); + expect(parseCliArgs(argv).fileNames?.[0]).to.equal('test1.txt'); + expect(parseCliArgs(argv).fileNames?.[1]).to.equal('test2.txt'); }); }); }); @@ -779,8 +779,8 @@ describe('arg-parser', function () { }); it('sets the filenames', function () { - expect(parseCliArgs(argv).fileNames[0]).to.equal('test1.js'); - expect(parseCliArgs(argv).fileNames[1]).to.equal('test2.js'); + expect(parseCliArgs(argv).fileNames?.[0]).to.equal('test1.js'); + expect(parseCliArgs(argv).fileNames?.[1]).to.equal('test2.js'); }); }); @@ -792,8 +792,8 @@ describe('arg-parser', function () { }); it('sets the filenames', function () { - expect(parseCliArgs(argv).fileNames[0]).to.equal('test1.mongodb'); - expect(parseCliArgs(argv).fileNames[1]).to.equal('test2.mongodb'); + expect(parseCliArgs(argv).fileNames?.[0]).to.equal('test1.mongodb'); + expect(parseCliArgs(argv).fileNames?.[1]).to.equal('test2.mongodb'); }); }); @@ -807,7 +807,7 @@ describe('arg-parser', function () { }); it('uses the remainder as filenames', function () { - expect(parseCliArgs(argv).fileNames[0]).to.equal('test2.txt'); + expect(parseCliArgs(argv).fileNames?.[0]).to.equal('test2.txt'); }); }); @@ -821,7 +821,7 @@ describe('arg-parser', function () { }); it('uses the remainder as filenames', function () { - expect(parseCliArgs(argv).fileNames[0]).to.equal('test2.txt'); + expect(parseCliArgs(argv).fileNames?.[0]).to.equal('test2.txt'); }); }); @@ -842,7 +842,7 @@ describe('arg-parser', function () { }); it('uses the remainder as filenames', function () { - expect(parseCliArgs(argv).fileNames[0]).to.equal( + expect(parseCliArgs(argv).fileNames?.[0]).to.equal( 'mongodb://domain.foo.js' ); }); @@ -892,7 +892,7 @@ describe('arg-parser', function () { }); context('when providing a deprecated argument', function () { - [ + for (const { deprecated, replacement, value } of [ { deprecated: 'ssl', replacement: 'tls' }, { deprecated: 'sslAllowInvalidCertificates', @@ -929,7 +929,7 @@ describe('arg-parser', function () { replacement: 'tlsDisabledProtocols', value: 'disabledProtos', }, - ].forEach(({ deprecated, replacement, value }) => { + ] as const) { it(`replaces --${deprecated} with --${replacement}`, function () { const argv = [...baseArgv, `--${deprecated}`]; if (value) { @@ -940,7 +940,7 @@ describe('arg-parser', function () { expect(args).to.not.have.property(deprecated); expect(args[replacement]).to.equal(value ?? true); }); - }); + } }); }); }); diff --git a/packages/cli-repl/src/async-repl.spec.ts b/packages/cli-repl/src/async-repl.spec.ts index 106ff9b07..04fa7aed5 100644 --- a/packages/cli-repl/src/async-repl.spec.ts +++ b/packages/cli-repl/src/async-repl.spec.ts @@ -173,9 +173,9 @@ describe('AsyncRepl', function () { exited = true; }); - let resolve; + let resolve!: () => void; repl.context.asyncFn = () => - new Promise((res) => { + new Promise((res) => { resolve = res; }); diff --git a/packages/cli-repl/src/cli-repl.spec.ts b/packages/cli-repl/src/cli-repl.spec.ts index a8855dc57..37b1a24bf 100644 --- a/packages/cli-repl/src/cli-repl.spec.ts +++ b/packages/cli-repl/src/cli-repl.spec.ts @@ -51,6 +51,7 @@ describe('CliRepl', function () { const tmpdir = useTmpdir(); async function log(): Promise { + if (!cliRepl.logWriter?.logFilePath) return []; await cliRepl.logWriter.flush(); // Ensure any pending data is written first return readReplLogfile(cliRepl.logWriter.logFilePath); } @@ -76,8 +77,8 @@ describe('CliRepl', function () { }); exitCode = null; - let resolveExitPromise; - exitPromise = new Promise((resolve) => { + let resolveExitPromise!: () => void; + exitPromise = new Promise((resolve) => { resolveExitPromise = resolve; }); @@ -858,7 +859,7 @@ describe('CliRepl', function () { try { await cliRepl.start('', {}); expect.fail('missed exception'); - } catch (err) { + } catch (err: any) { expect(err.message).to.equal('nested error'); } }); @@ -869,7 +870,7 @@ describe('CliRepl', function () { try { await cliRepl.start('', {}); expect.fail('missed exception'); - } catch (err) { + } catch (err: any) { expect(err.message).to.equal( 'Cannot use --json without --eval or with --shell or with extra files' ); @@ -884,7 +885,7 @@ describe('CliRepl', function () { try { await cliRepl.start('', {}); expect.fail('missed exception'); - } catch (err) { + } catch (err: any) { expect(err.message).to.equal( 'Cannot use --json without --eval or with --shell or with extra files' ); @@ -899,7 +900,7 @@ describe('CliRepl', function () { try { await cliRepl.start('', {}); expect.fail('missed exception'); - } catch (err) { + } catch (err: any) { expect(err.message).to.equal( 'Cannot use --json without --eval or with --shell or with extra files' ); @@ -1285,7 +1286,7 @@ describe('CliRepl', function () { it('does not emit warnings when connecting multiple times', async function () { await cliRepl.start(await testServer.connectionString(), {}); let warnings = 0; - const warningListener = (warning) => { + const warningListener = (warning: unknown) => { console.log('Unexpected warning', warning); warnings++; }; @@ -1407,7 +1408,9 @@ describe('CliRepl', function () { input.write('exit\n'); await waitBus(cliRepl.bus, 'mongosh:closed'); const useEvents = requests.flatMap((req) => - JSON.parse(req.body).batch.filter((entry) => entry.event === 'Use') + JSON.parse(req.body).batch.filter( + (entry: any) => entry.event === 'Use' + ) ); expect(useEvents).to.have.lengthOf(1); }); @@ -1425,7 +1428,9 @@ describe('CliRepl', function () { input.write('exit\n'); await waitBus(cliRepl.bus, 'mongosh:closed'); const useEvents = requests.flatMap((req) => - JSON.parse(req.body).batch.filter((entry) => entry.event === 'Use') + JSON.parse(req.body).batch.filter( + (entry: any) => entry.event === 'Use' + ) ); expect(useEvents).to.have.lengthOf(0); }); @@ -1448,7 +1453,9 @@ describe('CliRepl', function () { input.write('exit\n'); await waitBus(cliRepl.bus, 'mongosh:closed'); const useEvents = requests.flatMap((req) => - JSON.parse(req.body).batch.filter((entry) => entry.event === 'Use') + JSON.parse(req.body).batch.filter( + (entry: any) => entry.event === 'Use' + ) ); expect(useEvents).to.have.lengthOf(2); }); @@ -1469,7 +1476,7 @@ describe('CliRepl', function () { const loadEvents = requests .map((req) => JSON.parse(req.body).batch.filter( - (entry) => entry.event === 'Script Loaded' + (entry: any) => entry.event === 'Script Loaded' ) ) .flat(); @@ -1486,7 +1493,7 @@ describe('CliRepl', function () { const apiEvents = requests .map((req) => JSON.parse(req.body).batch.filter( - (entry) => entry.event === 'API Call' + (entry: any) => entry.event === 'API Call' ) ) .flat(); @@ -1500,12 +1507,12 @@ describe('CliRepl', function () { it('includes a statement about flushed telemetry in the log', async function () { await cliRepl.start(await testServer.connectionString(), {}); - const { logFilePath } = cliRepl.logWriter; + const { logFilePath } = cliRepl.logWriter!; input.write('db.hello()\n'); input.write('exit\n'); await waitBus(cliRepl.bus, 'mongosh:closed'); const flushEntry = (await readReplLogfile(logFilePath)).find( - (entry) => entry.id === 1_000_000_045 + (entry: any) => entry.id === 1_000_000_045 ); expect(flushEntry.attr.flushError).to.equal(null); expect(flushEntry.attr.flushDuration).to.be.a('number'); @@ -1522,7 +1529,7 @@ describe('CliRepl', function () { expect( requests .flatMap((req) => - JSON.parse(req.body).batch.map((entry) => entry.event) + JSON.parse(req.body).batch.map((entry: any) => entry.event) ) .sort() .filter(Boolean) @@ -1550,7 +1557,7 @@ describe('CliRepl', function () { const apiEvents = requests .map((req) => JSON.parse(req.body).batch.filter( - (entry) => entry.event === 'API Call' + (entry: any) => entry.event === 'API Call' ) ) .flat(); @@ -1712,7 +1719,7 @@ describe('CliRepl', function () { const connectEvents = requests.flatMap((req) => JSON.parse(req.body).batch.filter( - (entry) => entry.event === 'New Connection' + (entry: any) => entry.event === 'New Connection' ) ); expect(connectEvents).to.have.lengthOf(1); diff --git a/packages/cli-repl/src/format-output.spec.ts b/packages/cli-repl/src/format-output.spec.ts index 996c00ade..0a4a9e05f 100644 --- a/packages/cli-repl/src/format-output.spec.ts +++ b/packages/cli-repl/src/format-output.spec.ts @@ -510,7 +510,7 @@ test 558.79 GiB function () { for (const type of ['ExplainOutput', 'ExplainableCursor']) { it(`returns output with large depth (${type})`, function () { - const value = {}; + const value: Record = {}; let it = value; for (let i = 0; i <= 20; i++) { it = it[`level${i}`] = {}; diff --git a/packages/cli-repl/src/mongosh-repl.spec.ts b/packages/cli-repl/src/mongosh-repl.spec.ts index 0e7168c31..9b48c3f7e 100644 --- a/packages/cli-repl/src/mongosh-repl.spec.ts +++ b/packages/cli-repl/src/mongosh-repl.spec.ts @@ -71,7 +71,8 @@ describe('MongoshNodeRepl', function () { return 'success'; }); cp.listConfigOptions.callsFake(() => Object.keys(config)); - cp.exit.callsFake(((code) => bus.emit('test-exit-event', code)) as any); + cp.exit.callsFake(((code: number) => + bus.emit('test-exit-event', code)) as any); ioProvider = cp; @@ -91,8 +92,8 @@ describe('MongoshNodeRepl', function () { serviceProvider = sp; calledServiceProviderFunctions = () => Object.fromEntries( - Object.keys(sp) - .map((key) => [key, sp[key]?.callCount]) + Object.entries(sp) + .map(([key, value]) => [key, value?.callCount]) .filter(([, count]) => !!count) ); @@ -105,7 +106,7 @@ describe('MongoshNodeRepl', function () { mongoshRepl = new MongoshNodeRepl(mongoshReplOptions); }); - let originalEnvVars; + let originalEnvVars: Record; before(function () { originalEnvVars = { ...process.env }; }); @@ -527,7 +528,9 @@ describe('MongoshNodeRepl', function () { let getHistory: () => string[]; beforeEach(function () { - const { history } = mongoshRepl.runtimeState().repl as any; + const { history } = mongoshRepl.runtimeState().repl as unknown as { + history: string[]; + }; getHistory = () => history.filter((line) => !line.startsWith('prefill-')); for (let i = 0; i < prefill; i++) { @@ -827,10 +830,12 @@ describe('MongoshNodeRepl', function () { }); it('does not refresh the prompt if a window resize occurs while evaluating', async function () { - let resolveInProgress; - mongoshRepl.runtimeState().context.inProgress = new Promise((resolve) => { - resolveInProgress = resolve; - }); + let resolveInProgress!: () => void; + mongoshRepl.runtimeState().context.inProgress = new Promise( + (resolve) => { + resolveInProgress = resolve; + } + ); input.write('inProgress\n'); await tick(); @@ -933,7 +938,9 @@ describe('MongoshNodeRepl', function () { context('user prompts', function () { beforeEach(function () { // No boolean equivalent for 'passwordPrompt' in the API, so provide one: - mongoshRepl.runtimeState().context.booleanPrompt = (question) => { + mongoshRepl.runtimeState().context.booleanPrompt = ( + question: string + ) => { return Object.assign(mongoshRepl.onPrompt(question, 'yesno'), { [Symbol.for('@@mongosh.syntheticPromise')]: true, }); diff --git a/packages/cli-repl/src/tls-certificate-selector.spec.ts b/packages/cli-repl/src/tls-certificate-selector.spec.ts index db0438286..908de3851 100644 --- a/packages/cli-repl/src/tls-certificate-selector.spec.ts +++ b/packages/cli-repl/src/tls-certificate-selector.spec.ts @@ -16,7 +16,7 @@ describe('arg-mapper.applyTlsCertificateSelector', function () { ); exportCertificateAndPrivateKey = sinon.stub(); require(process.env.TEST_OS_EXPORT_CERTIFICATE_AND_KEY_PATH).setFn( - (search) => exportCertificateAndPrivateKey(search) + (search: unknown) => exportCertificateAndPrivateKey(search) ); }); afterEach(function () { diff --git a/packages/cli-repl/test/repl-helpers.ts b/packages/cli-repl/test/repl-helpers.ts index cf489a434..332b8a176 100644 --- a/packages/cli-repl/test/repl-helpers.ts +++ b/packages/cli-repl/test/repl-helpers.ts @@ -80,14 +80,17 @@ const fakeTTYProps: Partial = { isRaw: true, setRawMode(newValue: boolean) { this.isRaw = newValue; - return this; + return this as ReadStream & WriteStream; }, getColorDepth() { return 256; }, }; -async function readReplLogfile(logPath: string) { +async function readReplLogfile( + logPath?: string | null | undefined +): Promise { + if (!logPath) return []; return (await fs.readFile(logPath, 'utf8')) .split('\n') .filter((line) => line.trim())