From 27fdaad91fdf1a403f456c3cd22e6d07ea917679 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Fri, 16 Feb 2024 18:15:02 -0500 Subject: [PATCH] feat: pass item index into callback (#126) * feat: pass the item index into the callback * update readme --- README.md | 10 ++++++++++ cypress/e2e/passes-index.cy.ts | 13 +++++++++++++ package-lock.json | 35 ++++++---------------------------- src/index.js | 4 ++-- 4 files changed, 31 insertions(+), 31 deletions(-) create mode 100644 cypress/e2e/passes-index.cy.ts diff --git a/README.md b/README.md index 2356bc7..f3494f5 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,16 @@ it.each(selectors)('element %s is visible', (selector) => { // "element .new-todo is visible" ``` +## item index + +In addition to the item, the callback receives the index + +```js +it.each(selectors)('element %s is visible', (selector, k) => { + // k is 0, 1, 2, ... +}) +``` + ## Multiple arguments You can pass multiple arguments into the callback function by using an array of arrays. For example, to check if an element is visible, invisible, or exists, you can have both a selector and the assertion string for each item. diff --git a/cypress/e2e/passes-index.cy.ts b/cypress/e2e/passes-index.cy.ts new file mode 100644 index 0000000..27468b8 --- /dev/null +++ b/cypress/e2e/passes-index.cy.ts @@ -0,0 +1,13 @@ +// @ts-check +/// + +import '../..' + +describe('passes index', () => { + const items = ['foo', 'bar', 'baz'] + + it.each(items)('index %K', (item, k) => { + expect(k, `index ${k}`).to.be.within(0, items.length - 1) + expect(item, `item ${item}`).to.equal(items[k]) + }) +}) diff --git a/package-lock.json b/package-lock.json index 17f0656..6999923 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8122,21 +8122,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/semantic-release/node_modules/typescript": { - "version": "5.3.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", - "integrity": "sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==", - "dev": true, - "optional": true, - "peer": true, - "bin": { - "tsc": "bin/tsc", - "tsserver": "bin/tsserver" - }, - "engines": { - "node": ">=14.17" - } - }, "node_modules/semver": { "version": "7.5.3", "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.3.tgz", @@ -8759,9 +8744,9 @@ } }, "node_modules/typescript": { - "version": "4.8.4", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.8.4.tgz", - "integrity": "sha512-QCh+85mCy+h0IGff8r5XWzOVSbBO+KfeYrMQh7NJ58QujwcE22u+NUSmUxqF+un70P9GXKxa2HCNiTTMJknyjQ==", + "version": "4.9.5", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.5.tgz", + "integrity": "sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==", "dev": true, "bin": { "tsc": "bin/tsc", @@ -14745,14 +14730,6 @@ "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-4.10.2.tgz", "integrity": "sha512-anpAG63wSpdEbLwOqH8L84urkL6PiVIov3EMmgIhhThevh9aiMQov+6Btx0wldNcvm4wV+e2/Rt1QdDwKHFbHw==", "dev": true - }, - "typescript": { - "version": "5.3.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", - "integrity": "sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==", - "dev": true, - "optional": true, - "peer": true } } }, @@ -15239,9 +15216,9 @@ "dev": true }, "typescript": { - "version": "4.8.4", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.8.4.tgz", - "integrity": "sha512-QCh+85mCy+h0IGff8r5XWzOVSbBO+KfeYrMQh7NJ58QujwcE22u+NUSmUxqF+un70P9GXKxa2HCNiTTMJknyjQ==", + "version": "4.9.5", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.5.tgz", + "integrity": "sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==", "dev": true }, "uglify-js": { diff --git a/src/index.js b/src/index.js index 271002c..bc03f20 100644 --- a/src/index.js +++ b/src/index.js @@ -119,11 +119,11 @@ if (!it.each) { // define a test for each value if (Array.isArray(value)) { it(title, function itArrayCallback() { - return testCallback.apply(this, value) + return testCallback.apply(this, value, k) }) } else { it(title, function itCallback() { - return testCallback.call(this, value) + return testCallback.call(this, value, k) }) } }, this)