From d4c84691420fb89c72b3f7abe90c6f81e6848fee Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Tue, 5 Apr 2022 17:32:40 +0200 Subject: [PATCH 1/2] Explicitly type t.teardown() functions to return a promise This helps with the @typescript-eslint/no-misused-promises ESLint rule. --- test-d/teardown.ts | 6 ++++++ types/test-fn.d.ts | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 test-d/teardown.ts diff --git a/test-d/teardown.ts b/test-d/teardown.ts new file mode 100644 index 000000000..4627d1798 --- /dev/null +++ b/test-d/teardown.ts @@ -0,0 +1,6 @@ +import test from '..'; + +test('test', t => { + t.teardown(() => {}); // eslint-disable-line @typescript-eslint/no-empty-function + t.teardown(async () => {}); // eslint-disable-line @typescript-eslint/no-empty-function +}); diff --git a/types/test-fn.d.ts b/types/test-fn.d.ts index bcb9778ab..ff9d13fb7 100644 --- a/types/test-fn.d.ts +++ b/types/test-fn.d.ts @@ -46,7 +46,7 @@ export interface PlanFn { export type TimeoutFn = (ms: number, message?: string) => void; /** Declare a function to be run after the test has ended. */ -export type TeardownFn = (fn: () => void) => void; +export type TeardownFn = (fn: (() => Promise) | (() => void)) => void; export type ImplementationFn = ((t: ExecutionContext, ...args: Args) => PromiseLike) | From fb7614bc96e0f752749b46ef548e6deb24c0be1b Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Tue, 5 Apr 2022 17:33:27 +0200 Subject: [PATCH 2/2] Fix typing of plugin teardown The returned method always returns Promise. The teardown function itself may or may not return a promise. Support both to help with the @typescript-eslint/no-misused-promises ESLint rule. --- plugin.d.ts | 2 +- test-d/plugin.ts | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/plugin.d.ts b/plugin.d.ts index 11b2274aa..a2f68e179 100644 --- a/plugin.d.ts +++ b/plugin.d.ts @@ -41,7 +41,7 @@ export namespace SharedWorker { readonly file: string; publish: (data: Data) => PublishedMessage; subscribe: () => AsyncIterableIterator>; - teardown: void> (fn: TeardownFn) => TeardownFn; + teardown: (fn: (() => Promise) | (() => void)) => () => Promise; }; export namespace Plugin { diff --git a/test-d/plugin.ts b/test-d/plugin.ts index 368ab0295..edf1a0a0f 100644 --- a/test-d/plugin.ts +++ b/test-d/plugin.ts @@ -5,5 +5,13 @@ import * as plugin from '../plugin'; // eslint-disable-line import/extensions expectType(plugin.registerSharedWorker({filename: '', supportedProtocols: ['ava-4']})); const factory: plugin.SharedWorker.Factory = ({negotiateProtocol}) => { // eslint-disable-line @typescript-eslint/no-unused-vars - expectType(negotiateProtocol(['ava-4'])); + const protocol = negotiateProtocol(['ava-4']); + expectType(protocol); + + (async () => { + for await (const w of protocol.testWorkers()) { + expectType<() => Promise>(w.teardown(() => {})); // eslint-disable-line @typescript-eslint/no-empty-function + expectType<() => Promise>(w.teardown(async () => {})); // eslint-disable-line @typescript-eslint/no-empty-function + } + })(); };