Skip to content

Commit

Permalink
chore(cli-repl): use strict TS flag for test files (#2149)
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax authored Sep 4, 2024
1 parent 2c58118 commit 5682192
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 54 deletions.
2 changes: 1 addition & 1 deletion packages/cli-repl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 20 additions & 20 deletions packages/cli-repl/src/arg-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

Expand All @@ -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');
});
});

Expand All @@ -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');
});
});

Expand All @@ -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');
});
});

Expand All @@ -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');
});
});
});
Expand All @@ -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');
});
});

Expand All @@ -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');
});
});

Expand All @@ -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');
});
});

Expand All @@ -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');
});
});

Expand All @@ -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'
);
});
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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) {
Expand All @@ -940,7 +940,7 @@ describe('arg-parser', function () {
expect(args).to.not.have.property(deprecated);
expect(args[replacement]).to.equal(value ?? true);
});
});
}
});
});
});
4 changes: 2 additions & 2 deletions packages/cli-repl/src/async-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ describe('AsyncRepl', function () {
exited = true;
});

let resolve;
let resolve!: () => void;
repl.context.asyncFn = () =>
new Promise((res) => {
new Promise<void>((res) => {
resolve = res;
});

Expand Down
41 changes: 24 additions & 17 deletions packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('CliRepl', function () {
const tmpdir = useTmpdir();

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

let resolveExitPromise;
exitPromise = new Promise((resolve) => {
let resolveExitPromise!: () => void;
exitPromise = new Promise<void>((resolve) => {
resolveExitPromise = resolve;
});

Expand Down Expand Up @@ -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');
}
});
Expand All @@ -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'
);
Expand All @@ -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'
);
Expand All @@ -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'
);
Expand Down Expand Up @@ -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++;
};
Expand Down Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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');
Expand All @@ -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)
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-repl/src/format-output.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> = {};
let it = value;
for (let i = 0; i <= 20; i++) {
it = it[`level${i}`] = {};
Expand Down
27 changes: 17 additions & 10 deletions packages/cli-repl/src/mongosh-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
);

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

let originalEnvVars;
let originalEnvVars: Record<string, string | undefined>;
before(function () {
originalEnvVars = { ...process.env };
});
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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<void>(
(resolve) => {
resolveInProgress = resolve;
}
);
input.write('inProgress\n');
await tick();

Expand Down Expand Up @@ -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,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-repl/src/tls-certificate-selector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
Loading

0 comments on commit 5682192

Please sign in to comment.