From 9c9a6a1793cbbe8aa51167b7c258c9fc111f4df7 Mon Sep 17 00:00:00 2001 From: Igor Dlugosh Date: Mon, 8 May 2023 17:58:34 +0200 Subject: [PATCH] fix: add array of arrays type (#75) --- cypress/e2e/array-of-arrays.cy.ts | 9 +++++---- package.json | 2 +- src/index.d.ts | 22 ++++++++++++++++++---- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/cypress/e2e/array-of-arrays.cy.ts b/cypress/e2e/array-of-arrays.cy.ts index 61bcc34..ea639b9 100644 --- a/cypress/e2e/array-of-arrays.cy.ts +++ b/cypress/e2e/array-of-arrays.cy.ts @@ -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') }) }) diff --git a/package.json b/package.json index 2b0ea8b..4416fb3 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/index.d.ts b/src/index.d.ts index 44e42f0..92b1ac6 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -18,7 +18,13 @@ type TestCaseObject = { } declare namespace Mocha { - type TestCallback = (this: Context, arg0: T, arg1: any, arg2: any) => void + type TestCallback = 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 = (this: Context, arg0: T0) => void type TestCallback2 = (this: Context, arg0: T0, arg1: T1) => void type TestCallback3 = ( @@ -38,11 +44,19 @@ declare namespace Mocha { * @example it.each([1, 2, 3])('test %K', (x) => ...) * @see https://github.com/bahmutov/cypress-each */ + each( + values: Array, + totalChunks?: number, + chunkIndex?: number, + ): ( + titlePattern: string | TestTitleFn<[...T]>, + fn: TestCallback<[...T]>, + ) => void each( values: T[] | number, totalChunks?: number | ItemPredicateFunction, chunkIndex?: number, - ): (titlePattern: string | TestTitleFn, fn: TestCallback) => void + ): (titlePattern: string | TestTitleFn, fn: TestCallback<[T]>) => void /** * A single test case object where the keys are test titles, @@ -104,8 +118,8 @@ declare namespace Mocha { */ each( values: T[] | number, - totalChunks?: number, + totalChunks?: number | ItemPredicateFunction, chunkIndex?: number, - ): (titlePattern: string | TestTitleFn, fn: TestCallback) => void + ): (titlePattern: string | TestTitleFn, fn: TestCallback<[T]>) => void } }