Skip to content

Commit

Permalink
Improve typing of teardown methods
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
novemberborn committed Apr 10, 2022
1 parent 4257d28 commit 388ea53
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion plugin.d.ts
Expand Up @@ -41,7 +41,7 @@ export namespace SharedWorker {
readonly file: string;
publish: (data: Data) => PublishedMessage<Data>;
subscribe: () => AsyncIterableIterator<ReceivedMessage<Data>>;
teardown: <TeardownFn extends () => void> (fn: TeardownFn) => TeardownFn;
teardown: (fn: (() => Promise<void>) | (() => void)) => () => Promise<void>;
};

export namespace Plugin {
Expand Down
10 changes: 9 additions & 1 deletion test-d/plugin.ts
Expand Up @@ -5,5 +5,13 @@ import * as plugin from '../plugin'; // eslint-disable-line import/extensions
expectType<plugin.SharedWorker.Plugin.Protocol>(plugin.registerSharedWorker({filename: '', supportedProtocols: ['ava-4']}));

const factory: plugin.SharedWorker.Factory = ({negotiateProtocol}) => { // eslint-disable-line @typescript-eslint/no-unused-vars
expectType<plugin.SharedWorker.Protocol>(negotiateProtocol(['ava-4']));
const protocol = negotiateProtocol(['ava-4']);
expectType<plugin.SharedWorker.Protocol>(protocol);

(async () => {
for await (const w of protocol.testWorkers()) {
expectType<() => Promise<void>>(w.teardown(() => {})); // eslint-disable-line @typescript-eslint/no-empty-function
expectType<() => Promise<void>>(w.teardown(async () => {})); // eslint-disable-line @typescript-eslint/no-empty-function
}
})();
};
6 changes: 6 additions & 0 deletions 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
});
2 changes: 1 addition & 1 deletion types/test-fn.d.ts
Expand Up @@ -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)) => void;

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

0 comments on commit 388ea53

Please sign in to comment.