diff options
author | 2023-09-20 17:43:08 -0700 | |
---|---|---|
committer | 2023-09-20 17:43:08 -0700 | |
commit | 5c6d7760a5e706bcfd0421680bfa148fc50aec63 (patch) | |
tree | 63a9212085d20afea1017b2a4a4a383e7571aea9 | |
parent | 3c7b9353e10d56327223b1a4abfa812ebacea696 (diff) | |
download | bun-5c6d7760a5e706bcfd0421680bfa148fc50aec63.tar.gz bun-5c6d7760a5e706bcfd0421680bfa148fc50aec63.tar.zst bun-5c6d7760a5e706bcfd0421680bfa148fc50aec63.zip |
Improve types for `test.each`, `describe.each` (#5838)
* Improve types for each
* more
* remove
-rw-r--r-- | packages/bun-types/bun-test.d.ts | 20 | ||||
-rw-r--r-- | packages/bun-types/tests/test.test-d.ts | 49 |
2 files changed, 59 insertions, 10 deletions
diff --git a/packages/bun-types/bun-test.d.ts b/packages/bun-types/bun-test.d.ts index b32ffb4f7..6c22ad62f 100644 --- a/packages/bun-types/bun-test.d.ts +++ b/packages/bun-types/bun-test.d.ts @@ -174,18 +174,18 @@ declare module "bun:test" { * * @param table Array of Arrays with the arguments that are passed into the test fn for each row. */ - each<T extends ReadonlyArray<unknown>>( - table: ReadonlyArray<T>, + each<T extends Readonly<Readonly<[any, ...any[]]>[]>>( + table: T, ): ( label: string, - fn: (...args: T) => void | Promise<unknown>, + fn: (...args: Readonly<T>[number]) => void | Promise<unknown>, options?: number | TestOptions, ) => void; - each<T>( + each<T extends Array<any>>( table: ReadonlyArray<T>, ): ( label: string, - fn: (arg: T) => void | Promise<unknown>, + fn: (...args: Readonly<T>) => void | Promise<unknown>, options?: number | TestOptions, ) => void; }; @@ -419,18 +419,18 @@ declare module "bun:test" { * * @param table Array of Arrays with the arguments that are passed into the test fn for each row. */ - each<T extends ReadonlyArray<unknown>>( - table: ReadonlyArray<T>, + each<T extends Readonly<Readonly<[any, ...any[]]>[]>>( + table: T, ): ( label: string, - fn: (...args: T) => void | Promise<unknown>, + fn: (...args: Readonly<T>[number]) => void | Promise<unknown>, options?: number | TestOptions, ) => void; - each<T>( + each<T extends Array<any>>( table: ReadonlyArray<T>, ): ( label: string, - fn: (arg: T, done: (err?: unknown) => void) => void | Promise<unknown>, + fn: (...args: Readonly<T>) => void | Promise<unknown>, options?: number | TestOptions, ) => void; }; diff --git a/packages/bun-types/tests/test.test-d.ts b/packages/bun-types/tests/test.test-d.ts index 831b18e2e..1e67590b2 100644 --- a/packages/bun-types/tests/test.test-d.ts +++ b/packages/bun-types/tests/test.test-d.ts @@ -63,3 +63,52 @@ describe("bun:test", () => { expect(undefined).not.toBeDefined(); }); }); + +// inference should work when data is passed directly in +test.each([ + ["a", true, 5], + ["b", false, 1234], +])("test.each", (a, b, c) => { + expectType<string>(a); + expectType<boolean>(b); + expectType<number | string>(c); +}); +describe.each([ + ["a", true, 5], + ["b", false, "asdf"], +])("test.each", (a, b, c) => { + expectType<string>(a); + expectType<boolean>(b); + expectType<number | string>(c); +}); + +// no inference on data +const data = [ + ["a", true, 5], + ["b", false, "asdf"], +]; +test.each(data)("test.each", (...args) => { + expectType<string | number | boolean>(args[0]); +}); +describe.each(data)("test.each", (a, b, c) => { + expectType<string | number | boolean>(a); + expectType<string | number | boolean>(b); + expectType<string | number | boolean>(c); +}); + +// as const +const dataAsConst = [ + ["a", true, 5], + ["b", false, "asdf"], +] as const; + +test.each(dataAsConst)("test.each", (...args) => { + expectType<string>(args[0]); + expectType<boolean>(args[1]); + expectType<string | number>(args[2]); +}); +describe.each(dataAsConst)("test.each", (...args) => { + expectType<string>(args[0]); + expectType<boolean>(args[1]); + expectType<string | number>(args[2]); +}); |