From 5c6d7760a5e706bcfd0421680bfa148fc50aec63 Mon Sep 17 00:00:00 2001 From: Colin McDonnell Date: Wed, 20 Sep 2023 17:43:08 -0700 Subject: [PATCH] Improve types for `test.each`, `describe.each` (#5838) * Improve types for each * more * remove --- packages/bun-types/bun-test.d.ts | 20 +++++----- 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 b32ffb4f7b..6c22ad62f5 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>( - table: ReadonlyArray, + each[]>>( + table: T, ): ( label: string, - fn: (...args: T) => void | Promise, + fn: (...args: Readonly[number]) => void | Promise, options?: number | TestOptions, ) => void; - each( + each>( table: ReadonlyArray, ): ( label: string, - fn: (arg: T) => void | Promise, + fn: (...args: Readonly) => void | Promise, 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>( - table: ReadonlyArray, + each[]>>( + table: T, ): ( label: string, - fn: (...args: T) => void | Promise, + fn: (...args: Readonly[number]) => void | Promise, options?: number | TestOptions, ) => void; - each( + each>( table: ReadonlyArray, ): ( label: string, - fn: (arg: T, done: (err?: unknown) => void) => void | Promise, + fn: (...args: Readonly) => void | Promise, 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 831b18e2e4..1e67590b20 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(a); + expectType(b); + expectType(c); +}); +describe.each([ + ["a", true, 5], + ["b", false, "asdf"], +])("test.each", (a, b, c) => { + expectType(a); + expectType(b); + expectType(c); +}); + +// no inference on data +const data = [ + ["a", true, 5], + ["b", false, "asdf"], +]; +test.each(data)("test.each", (...args) => { + expectType(args[0]); +}); +describe.each(data)("test.each", (a, b, c) => { + expectType(a); + expectType(b); + expectType(c); +}); + +// as const +const dataAsConst = [ + ["a", true, 5], + ["b", false, "asdf"], +] as const; + +test.each(dataAsConst)("test.each", (...args) => { + expectType(args[0]); + expectType(args[1]); + expectType(args[2]); +}); +describe.each(dataAsConst)("test.each", (...args) => { + expectType(args[0]); + expectType(args[1]); + expectType(args[2]); +});