Skip to content

Commit

Permalink
[jest]: Restore ExtractEachCallbackArgs and add tests (DefinitelyTy…
Browse files Browse the repository at this point in the history
…ped#64739)

Co-authored-by: Jake Bailey <[email protected]>
  • Loading branch information
FloEdelmann and jakebailey authored Mar 15, 2023
1 parent 62061c3 commit bf59992
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
37 changes: 36 additions & 1 deletion types/jest/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,41 @@ declare var xtest: jest.It;

declare const expect: jest.Expect;

// Remove once https://github.com/microsoft/TypeScript/issues/53255 is fixed.
type ExtractEachCallbackArgs<T extends ReadonlyArray<any>> = {
1: [T[0]];
2: [T[0], T[1]];
3: [T[0], T[1], T[2]];
4: [T[0], T[1], T[2], T[3]];
5: [T[0], T[1], T[2], T[3], T[4]];
6: [T[0], T[1], T[2], T[3], T[4], T[5]];
7: [T[0], T[1], T[2], T[3], T[4], T[5], T[6]];
8: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7]];
9: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7], T[8]];
10: [T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7], T[8], T[9]];
fallback: Array<T extends ReadonlyArray<infer U> ? U : any>;
}[T extends Readonly<[any]>
? 1
: T extends Readonly<[any, any]>
? 2
: T extends Readonly<[any, any, any]>
? 3
: T extends Readonly<[any, any, any, any]>
? 4
: T extends Readonly<[any, any, any, any, any]>
? 5
: T extends Readonly<[any, any, any, any, any, any]>
? 6
: T extends Readonly<[any, any, any, any, any, any, any]>
? 7
: T extends Readonly<[any, any, any, any, any, any, any, any]>
? 8
: T extends Readonly<[any, any, any, any, any, any, any, any, any]>
? 9
: T extends Readonly<[any, any, any, any, any, any, any, any, any, any]>
? 10
: 'fallback'];

type FakeableAPI =
| 'Date'
| 'hrtime'
Expand Down Expand Up @@ -482,7 +517,7 @@ declare namespace jest {
) => void;
<T extends ReadonlyArray<any>>(cases: ReadonlyArray<T>): (
name: string,
fn: (...args: T) => any,
fn: (...args: ExtractEachCallbackArgs<T>) => any,
timeout?: number,
) => void;
// Not arrays.
Expand Down
21 changes: 19 additions & 2 deletions types/jest/jest-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1778,17 +1778,25 @@ test.each([

declare const constCases: [['a', 'b', 'ab'], ['d', 2, 'd2']];
test.each(constCases)('%s + %s', (...args) => {
// $ExpectType ["a", "b", "ab"] | ["d", 2, "d2"]
// following assertion is skipped because of flaky testing
// _$ExpectType ["a", "b", "ab"] | ["d", 2, "d2"]
args;
});
test.each(constCases)('%s + %s', (a, b, c) => {
a; // $ExpectType "a" | "d"
// following assertion is skipped because of flaky testing
b; // _$ExpectType "b" | 2
c; // $ExpectType "ab" | "d2"
});

declare const constCasesWithMoreThanTen: [
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[91, 92, 93, 94, 95, 96, 97, 98, 99, 910, 911],
];

test.each(constCasesWithMoreThanTen)('should fall back with more than 10 args', (...args) => {
// $ExpectType [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] | [91, 92, 93, 94, 95, 96, 97, 98, 99, 910, 911]
// following assertion is skipped because of flaky testing
// _$ExpectType [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] | [91, 92, 93, 94, 95, 96, 97, 98, 99, 910, 911]
args;
});

Expand Down Expand Up @@ -1822,6 +1830,15 @@ test.each([
b; // $ExpectType string
});

test.each([
[1, '1'],
[2, '2'],
] as const)('', (a, b) => {
// following assertion is skipped because of flaky testing
a; // _$ExpectType 1 | 2
b; // $ExpectType "1" | "2"
});

test.only.each([
[1, 1, 2],
[1, 2, 3],
Expand Down

0 comments on commit bf59992

Please sign in to comment.