Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typing of teardown methods #3003

Merged
merged 2 commits into from Apr 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
teardown: (fn: (() => Promise<void>) | (() => void)) => () => Promise<void>;
teardown: (fn: (() => Promise<void> | void) => () => Promise<void>;

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would that be fine or would void "nullify" Promise<void>? Or would it be clearer to have overloads?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm: microsoft/TypeScript#43921

I guess what you have is fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really know if overloads will make any difference.

};

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