fix: improve types
Various improvements to the TypeScript types:
- Add
TestOptions.only - Add
doneparam toTestFn - Update
testReturnType toPromise<void> - Remove
ItContextand replace param with initfn type withdone - Add exported hooks
- Change
TestContext.testtotypeof test - Added
TestContext.runOnly,TestContext.beforeEach,TestContext.afterEach,TestContext.name - Formatfile with prettier + prettier-plugin-jsdoc
- Make all exported fn params optional
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:
- Add
describe.skipdescribe.todoit.skipit.skip - Open PR to improve
@types/nodeand sync?
If these are welcome, happy to contribute another PR addressing those.
@MoLow thanks for the review ~I can't find any reference for the fn return type signature of TestFn though?~
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:
't' is declared but its value is never read. ts(6133)
Parameter 't' implicitly has an 'any' type, but a better type may be inferred from usage. ts(7044)
Making TestFn generic doesn't change much.
In order to test these changes, I wrote a quick tsd test in test/test.test-d.ts:
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 package.json:
{
"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 test:types to the test script) in a separate PR.