mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
Improve types for test.each, describe.each (#5838)
* Improve types for each * more * remove
This commit is contained in:
20
packages/bun-types/bun-test.d.ts
vendored
20
packages/bun-types/bun-test.d.ts
vendored
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user