Improve types for test.each, describe.each (#5838)

* Improve types for each

* more

* remove
This commit is contained in:
Colin McDonnell
2023-09-20 17:43:08 -07:00
committed by GitHub
parent 3c7b9353e1
commit 5c6d7760a5
2 changed files with 59 additions and 10 deletions

View File

@@ -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;
};

View File

@@ -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]);
});