Skip to content

Commit e1b8213

Browse files
committed
fix(test): handle nullable Worker.onmessage in type constraints and call sites
Worker.onmessage is typed as `((ev: MessageEvent) => any) | null`, so the existing MethodKeyOf<T> utility (which only maps non-nullable functions) caused type errors when used with Worker. Introduce NullableMethodKeyOf<T> that also matches `T[K] extends AnyFn | null` and apply it to the unimplemented-method list in mock_test.ts. Add non-null assertions at the three call sites in cli_test.ts where onmessage is invoked directly, since the fake worker always provides a value there.
1 parent 5cfca39 commit e1b8213

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

denops/@denops-private/cli_test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ Deno.test("main()", async (t) => {
288288
);
289289

290290
await t.step("and the worker stream is closed", async (t) => {
291-
fakeWorker.onmessage(new MessageEvent("message", { data: null }));
291+
fakeWorker.onmessage!(new MessageEvent("message", { data: null }));
292292
await delay(0);
293293

294294
await t.step("calls Worker.terminate()", () => {
@@ -453,7 +453,7 @@ Deno.test("main()", async (t) => {
453453
assertSpyCalls(globalThis_Worker, 1);
454454
fakeTcpListener.close();
455455

456-
fakeWorker.onmessage(new MessageEvent("message", { data: null }));
456+
fakeWorker.onmessage!(new MessageEvent("message", { data: null }));
457457
await delay(0);
458458

459459
await t.step("calls Worker.terminate()", () => {
@@ -557,7 +557,7 @@ Deno.test("main()", async (t) => {
557557
await delay(0);
558558

559559
await t.step("and the worker stream is closed", async (t) => {
560-
fakeWorker.onmessage(new MessageEvent("message", { data: null }));
560+
fakeWorker.onmessage!(new MessageEvent("message", { data: null }));
561561
await delay(0);
562562

563563
await t.step("outputs error logs", () => {

tests/denops/testutil/mock_test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ type MethodKeyOf<T extends AnyRecord> = ({
2424
[K in keyof T]: T[K] extends AnyFn ? K : never;
2525
})[keyof T];
2626

27+
type NullableMethodKeyOf<T extends AnyRecord> = ({
28+
[K in keyof T]: T[K] extends AnyFn | null ? K : never;
29+
})[keyof T];
30+
2731
type GetterKeyOf<T extends AnyRecord> = ({
2832
[K in keyof T]: T[K] extends AnyFn ? never : K;
2933
})[keyof T];
@@ -337,7 +341,7 @@ Deno.test("createFakeWorker()", async (t) => {
337341
"removeEventListener",
338342
"dispatchEvent",
339343
"terminate",
340-
] as const satisfies readonly MethodKeyOf<Worker>[];
344+
] as const satisfies readonly NullableMethodKeyOf<Worker>[];
341345
for (const key of unimplementedMethods) {
342346
await t.step(`.${key}()`, () => {
343347
assertThrows(() => (worker[key] as AnyFn)(), Error, "Unimplemented");

0 commit comments

Comments
 (0)