Skip to content

Commit

Permalink
chore(node-runtime-worker-thread): use strict TS flag for tests (#2155)
Browse files Browse the repository at this point in the history
... and remove the non-strict TS config from this repo entirely 🎉
  • Loading branch information
addaleax authored Sep 5, 2024
1 parent 1685c71 commit b741c79
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 51 deletions.
3 changes: 1 addition & 2 deletions configs/tsconfig-mongosh/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"description": "Shared Mongosh Typescript configuration",
"license": "SSPL",
"files": [
"tsconfig.common.json",
"tsconfig.test.json"
"tsconfig.common.json"
],
"peerDependencies": {
"typescript": "^5.3.3"
Expand Down
13 changes: 0 additions & 13 deletions configs/tsconfig-mongosh/tsconfig.test.json

This file was deleted.

2 changes: 1 addition & 1 deletion packages/node-runtime-worker-thread/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"node": ">=14.15.1"
},
"scripts": {
"test": "cross-env TS_NODE_PROJECT=./tsconfig.test.json mocha -r \"../../scripts/import-expansions.js\" -r \"./tests/register-worker.js \" --timeout 15000 -r ts-node/register \"./src/**/*.spec.ts\"",
"test": "mocha -r \"../../scripts/import-expansions.js\" -r \"./tests/register-worker.js \" --timeout 15000 -r ts-node/register \"./src/**/*.spec.ts\"",
"pretest-ci": "node ../../scripts/run-if-package-requested.js npm run webpack-build -- --no-stats --no-devtool",
"test-ci": "node ../../scripts/run-if-package-requested.js npm test",
"test-coverage": "nyc --no-clean --cwd ../.. --reporter=none npm run test",
Expand Down
8 changes: 4 additions & 4 deletions packages/node-runtime-worker-thread/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('WorkerRuntime', function () {
nodb: true,
});

let err: Error;
let err!: Error;

try {
await runtime.evaluate('throw new TypeError("Oh no, types!")');
Expand Down Expand Up @@ -219,7 +219,7 @@ describe('WorkerRuntime', function () {
const runtime = new WorkerRuntime('mongodb://nodb/', dummyOptions, {
nodb: true,
});
let err: Error;
let err!: Error;
try {
await Promise.all([
runtime.evaluate('while(true){}'),
Expand All @@ -246,7 +246,7 @@ describe('WorkerRuntime', function () {

await runtime.waitForRuntimeToBeReady();

let err: Error;
let err!: Error;

try {
await Promise.all([
Expand Down Expand Up @@ -329,7 +329,7 @@ describe('WorkerRuntime', function () {

await runtime.waitForRuntimeToBeReady();

let err: Error;
let err!: Error;

try {
await Promise.all([
Expand Down
2 changes: 1 addition & 1 deletion packages/node-runtime-worker-thread/src/lock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Lock', function () {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
lock.lock();

let err: Error;
let err!: Error;

try {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
Expand Down
12 changes: 6 additions & 6 deletions packages/node-runtime-worker-thread/src/rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('rpc', function () {
woof(...args: any[]): string;
neverResolves(...args: any[]): void;
}>;
let exposed: Exposed<unknown> | null;
let exposed: Exposed<unknown>; // adding `| null` breaks TS type inference

afterEach(function () {
if (messageBus) {
Expand All @@ -36,12 +36,12 @@ describe('rpc', function () {

if (caller) {
caller[cancel]();
caller = null;
caller = null as any;
}

if (exposed) {
exposed[close]();
exposed = null;
exposed = null as any;
}
});

Expand Down Expand Up @@ -74,7 +74,7 @@ describe('rpc', function () {
messageBus
);

let err: Error;
let err!: Error;

try {
// eslint-disable-next-line @typescript-eslint/await-thenable
Expand Down Expand Up @@ -136,7 +136,7 @@ describe('rpc', function () {
messageBus = createMockRpcMesageBus();
caller = createCaller(['meow'], messageBus);

messageBus.addEventListener('message', (data) => {
messageBus.addEventListener('message', (data: unknown) => {
expect(data).to.have.property('func', 'meow');
done();
});
Expand All @@ -150,7 +150,7 @@ describe('rpc', function () {
it('stops all in-flight evaluations', async function () {
messageBus = createMockRpcMesageBus();
caller = createCaller(['neverResolves'], messageBus);
let err: Error;
let err!: Error;
try {
await Promise.all([
caller.neverResolves(),
Expand Down
5 changes: 4 additions & 1 deletion packages/node-runtime-worker-thread/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export type Exposed<T> = { [k in keyof T]: T[k] & { close(): void } } & {
[close]: () => void;
};

export function exposeAll<O>(obj: O, messageBus: RPCMessageBus): Exposed<O> {
export function exposeAll<O extends object>(
obj: O,
messageBus: RPCMessageBus
): Exposed<O> {
Object.entries(obj as Record<string, any>).forEach(([key, val]) => {
const { close } = expose(
key,
Expand Down
24 changes: 12 additions & 12 deletions packages/node-runtime-worker-thread/src/worker-runtime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ describe('worker-runtime', function () {

if (caller) {
caller[cancel]();
caller = null;
caller = null as any;
}
});

it('should throw if worker is not initialized yet', async function () {
const { evaluate } = caller;

let err: Error;
let err!: Error;

try {
await evaluate('1 + 1');
Expand Down Expand Up @@ -161,18 +161,18 @@ describe('worker-runtime', function () {
describe('shell-api results', function () {
const testServer = startSharedTestServer();
const db = `test-db-${Date.now().toString(16)}`;
let exposed: Exposed<unknown> | null;
let exposed: Exposed<unknown>; // adding `| null` breaks TS type inference

afterEach(function () {
if (exposed) {
exposed[close]();
exposed = null;
exposed = null as any;
}
});

type CommandTestRecord =
| [string | string[], string]
| [string | string[], string, any];
| [string | string[], string | null, any];

const showCommand: CommandTestRecord[] = [
[
Expand Down Expand Up @@ -323,11 +323,11 @@ describe('worker-runtime', function () {
.forEach((testCase) => {
const [commands, resultType, printable] = testCase;

let command: string | undefined;
let command: string;
let prepare: undefined | string[];

if (Array.isArray(commands)) {
command = commands.pop();
command = commands.pop()!;
prepare = commands;
} else {
command = commands;
Expand Down Expand Up @@ -376,7 +376,7 @@ describe('worker-runtime', function () {

await init('mongodb://nodb/', dummyOptions, { nodb: true });

let err: Error;
let err!: Error;
try {
await evaluate('throw new TypeError("Oh no, types!")');
} catch (e: any) {
Expand All @@ -396,7 +396,7 @@ describe('worker-runtime', function () {

await init('mongodb://nodb/', dummyOptions, { nodb: true });

let err: Error;
let err!: Error;
try {
await evaluate(
'throw Object.assign(new TypeError("Oh no, types!"), { errInfo: { message: "wrong type :S" } })'
Expand Down Expand Up @@ -430,7 +430,7 @@ describe('worker-runtime', function () {
const { init, evaluate } = caller;
await init('mongodb://nodb/', dummyOptions, { nodb: true });

let err: Error;
let err!: Error;

try {
await Promise.all([
Expand Down Expand Up @@ -665,7 +665,7 @@ describe('worker-runtime', function () {

await init('mongodb://nodb/', dummyOptions, { nodb: true });

let err: Error;
let err!: Error;

try {
await Promise.all([
Expand Down Expand Up @@ -695,7 +695,7 @@ describe('worker-runtime', function () {

await init('mongodb://nodb/', dummyOptions, { nodb: true });

let err: Error;
let err!: Error;

try {
await Promise.all([
Expand Down
11 changes: 0 additions & 11 deletions packages/node-runtime-worker-thread/tsconfig.test.json

This file was deleted.

0 comments on commit b741c79

Please sign in to comment.