-
Notifications
You must be signed in to change notification settings - Fork 10
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
fix: improve types #42
base: main
Are you sure you want to change the base?
Conversation
@@ -29,40 +35,114 @@ interface TestOptions { | |||
signal?: AbortSignal; | |||
} | |||
|
|||
type TestFn = (t: TestContext) => any | Promise<any>; | |||
type TestFn = (t: TestContext, done: (result?: any) => void) => any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type TestFn = (t: TestContext, done: (result?: any) => void) => any; | |
type TestFn = <T>(t: TestContext, done: (result?: T) => void) => any | <T>(t: TestContext) => Promise<T>; |
lib/test.d.ts
Outdated
export function test( | ||
name: string, | ||
options: TestOptions, | ||
fn: TestFn |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export function test( | |
name: string, | |
options: TestOptions, | |
fn: TestFn | |
export function test<T>( | |
name: string, | |
options: TestOptions, | |
fn: TestFn<T> |
lib/test.d.ts
Outdated
export function test(name: string, fn: TestFn): Promise<void>; | ||
export function test(options: TestOptions, fn: TestFn): Promise<void>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export function test(name: string, fn: TestFn): Promise<void>; | |
export function test(options: TestOptions, fn: TestFn): Promise<void>; | |
export function test<T>(name: string, fn: TestFn<T>): Promise<void>; | |
export function test<T>(options: TestOptions, fn: TestFn<T>): Promise<void>; |
Using .prettierrc: ```json { "plugins": ["prettier-plugin-jsdoc"] } ```
@MoLow thanks for the review Edit: misinterpreted your suggestion 🤦 will apply your changes! Edit2: couldn't get this to work, tried: type TestFn =
| ((t: TestContext, done: (result?: any) => void) => any)
| ((t: TestContext) => Promise<any>)
test(async (t) => {}) Which yields:
Making TestFn generic doesn't change much. |
In order to test these changes, I wrote a quick import { expectType } from "tsd";
import test, { describe, it } from "../lib/test.js";
// # test()
// Parameters
expectType<void>(await test());
expectType<void>(await test("name", { only: true }));
expectType<void>(await test("name", { only: true }, () => {}));
expectType<void>(await test({ only: true }));
expectType<void>(await test({ only: true }, () => {}));
expectType<void>(await test("name", () => {}));
expectType<void>(await test(() => {}));
// TestFn
expectType<Promise<void>>(test(() => {}));
expectType<void>(await test((t) => {}));
expectType<void>(await test(async (t) => {}));
expectType<void>(await test((t, done) => done()));
expectType<void>(await test(async (t, done) => done())); // Test will fail
// # describe()
// Parameters
expectType<void>(describe());
expectType<void>(describe("name", { only: true }));
expectType<void>(describe("name", { only: true }, () => {}));
expectType<void>(describe({ only: true }));
expectType<void>(describe({ only: true }, () => {}));
expectType<void>(describe("name", () => {}));
expectType<void>(describe(() => {}));
// # it()
// Parameters
expectType<void>(it());
expectType<void>(it("name", { only: true }));
expectType<void>(it("name", { only: true }, () => {}));
expectType<void>(it({ only: true }));
expectType<void>(it({ only: true }, () => {}));
expectType<void>(it("name", () => {}));
expectType<void>(it(() => {})); In {
"scripts": {
"test:types": "tsd --files test/test.test-d.ts"
}
} And run with: npm i -D tsd
npm run test:types If wanted, I will commit these changes (and add |
Various improvements to the TypeScript types:
TestOptions.only
done
param toTestFn
test
ReturnType toPromise<void>
ItContext
and replace param with init
fn type withdone
TestContext.test
totypeof test
TestContext.runOnly
,TestContext.beforeEach
,TestContext.afterEach
,TestContext.name
I tried re-using the types from @types/node but these also seem incomplete/incorrect at places (e.g.
SuiteFn
) so instead based these changes on the README of this package.Suggestions for further improvements:
describe.skip
describe.todo
it.skip
it.skip
@types/node
and sync?If these are welcome, happy to contribute another PR addressing those.