Skip to content

Commit

Permalink
fix: add array of arrays type (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
DlgSHi authored May 8, 2023
1 parent d6d26d9 commit 9c9a6a1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
9 changes: 5 additions & 4 deletions cypress/e2e/array-of-arrays.cy.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import '../../src'

// https://github.com/bahmutov/cypress-each/issues/2
describe('Array of arrays', () => {
it.each([
[1, 'foo'],
[2, 'bar'],
])('test with %d %s', (a, b) => {
[1, 'foo', true],
[2, 'bar', false],
])('test with %d %s', (a: number, b: string, c: boolean) => {
// a should be a number
// b should be a string
// b should be a boolean
expect(a, 'first argument').to.be.a('number')
expect(b, 'second argument').to.be.a('string')
expect(c, 'third argument').to.be.a('boolean')
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"src"
],
"scripts": {
"lint": "tsc --pretty --allowJs --strict --noEmit src/index.js cypress/**/*.js cypress/**/*.ts",
"lint": "tsc --pretty --allowJs --strict --noEmit src/index.js cypress/**/*.ts",
"test": "cypress-expect run --expect cypress/expected.json",
"semantic-release": "semantic-release"
},
Expand Down
22 changes: 18 additions & 4 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ type TestCaseObject<T> = {
}

declare namespace Mocha {
type TestCallback<T> = (this: Context, arg0: T, arg1: any, arg2: any) => void
type TestCallback<T extends readonly any[]> = T extends []
? (this: Context, arg1: any, arg2: any) => void
: Parameters<(...res: [...T, any, any]) => void> extends [...infer R]
? R extends readonly [...T, any, any]
? (this: Context, ...res: [...R]) => void
: never
: never
type TestCallback1<T0> = (this: Context, arg0: T0) => void
type TestCallback2<T0, T1> = (this: Context, arg0: T0, arg1: T1) => void
type TestCallback3<T0, T1, T2> = (
Expand All @@ -38,11 +44,19 @@ declare namespace Mocha {
* @example it.each([1, 2, 3])('test %K', (x) => ...)
* @see https://github.com/bahmutov/cypress-each
*/
each<T extends readonly [...T]>(
values: Array<readonly [...T]>,
totalChunks?: number,
chunkIndex?: number,
): (
titlePattern: string | TestTitleFn<[...T]>,
fn: TestCallback<[...T]>,
) => void
each<T = unknown>(
values: T[] | number,
totalChunks?: number | ItemPredicateFunction<T>,
chunkIndex?: number,
): (titlePattern: string | TestTitleFn<T>, fn: TestCallback<T>) => void
): (titlePattern: string | TestTitleFn<T>, fn: TestCallback<[T]>) => void

/**
* A single test case object where the keys are test titles,
Expand Down Expand Up @@ -104,8 +118,8 @@ declare namespace Mocha {
*/
each<T = unknown>(
values: T[] | number,
totalChunks?: number,
totalChunks?: number | ItemPredicateFunction<T>,
chunkIndex?: number,
): (titlePattern: string | TestTitleFn<T>, fn: TestCallback<T>) => void
): (titlePattern: string | TestTitleFn<T>, fn: TestCallback<[T]>) => void
}
}

0 comments on commit 9c9a6a1

Please sign in to comment.