Skip to content

Commit 388ea53

Browse files
authored
Improve typing of teardown methods
* Explicitly type t.teardown() functions to return a promise This helps with the @typescript-eslint/no-misused-promises ESLint rule. * Fix typing of plugin teardown The returned method always returns Promise<void>. The teardown function itself may or may not return a promise. Support both to help with the @typescript-eslint/no-misused-promises ESLint rule.
1 parent 4257d28 commit 388ea53

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

plugin.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export namespace SharedWorker {
4141
readonly file: string;
4242
publish: (data: Data) => PublishedMessage<Data>;
4343
subscribe: () => AsyncIterableIterator<ReceivedMessage<Data>>;
44-
teardown: <TeardownFn extends () => void> (fn: TeardownFn) => TeardownFn;
44+
teardown: (fn: (() => Promise<void>) | (() => void)) => () => Promise<void>;
4545
};
4646

4747
export namespace Plugin {

test-d/plugin.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,13 @@ import * as plugin from '../plugin'; // eslint-disable-line import/extensions
55
expectType<plugin.SharedWorker.Plugin.Protocol>(plugin.registerSharedWorker({filename: '', supportedProtocols: ['ava-4']}));
66

77
const factory: plugin.SharedWorker.Factory = ({negotiateProtocol}) => { // eslint-disable-line @typescript-eslint/no-unused-vars
8-
expectType<plugin.SharedWorker.Protocol>(negotiateProtocol(['ava-4']));
8+
const protocol = negotiateProtocol(['ava-4']);
9+
expectType<plugin.SharedWorker.Protocol>(protocol);
10+
11+
(async () => {
12+
for await (const w of protocol.testWorkers()) {
13+
expectType<() => Promise<void>>(w.teardown(() => {})); // eslint-disable-line @typescript-eslint/no-empty-function
14+
expectType<() => Promise<void>>(w.teardown(async () => {})); // eslint-disable-line @typescript-eslint/no-empty-function
15+
}
16+
})();
917
};

test-d/teardown.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import test from '..';
2+
3+
test('test', t => {
4+
t.teardown(() => {}); // eslint-disable-line @typescript-eslint/no-empty-function
5+
t.teardown(async () => {}); // eslint-disable-line @typescript-eslint/no-empty-function
6+
});

types/test-fn.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface PlanFn {
4646
export type TimeoutFn = (ms: number, message?: string) => void;
4747

4848
/** Declare a function to be run after the test has ended. */
49-
export type TeardownFn = (fn: () => void) => void;
49+
export type TeardownFn = (fn: (() => Promise<void>) | (() => void)) => void;
5050

5151
export type ImplementationFn<Args extends unknown[], Context = unknown> =
5252
((t: ExecutionContext<Context>, ...args: Args) => PromiseLike<void>) |

0 commit comments

Comments
 (0)