diff --git a/README.md b/README.md index 65d06d9..8894d76 100644 --- a/README.md +++ b/README.md @@ -206,6 +206,15 @@ it.each(items, (x, k) => ...) // creates a test for every item the predicate returns a truthy value ``` +## Return value + +`it.each(...)(...)` and `describe.each(...)(...)` return the number of created tests. + +```js +const n = it.each([1, 2])(...) +// n is 2 +``` + ## Exclusive tests Normally you could run just a selected test using `it.only` or a suite of tests using `describe.only`. Similarly, you could skip a single test or a suite of tests using `it.skip` and `describe.skip` methods. These methods are NOT supported by `it.each` and `describe.each`. Thus if you want to only run the `it.each` tests, surround it with its own `describe` block. diff --git a/cypress/integration/describe-each-spec.js b/cypress/integration/describe-each-spec.js index ad7d1a3..a79ac6b 100644 --- a/cypress/integration/describe-each-spec.js +++ b/cypress/integration/describe-each-spec.js @@ -3,7 +3,10 @@ import '../..' -describe.each(['A', 1])('%s', (x) => { +const n = describe.each(['A', 1])('%s', (x) => { + before(() => { + expect(n, 'number of created suites').to.equal(2) + }) // we can use the values passed into the "describe" callback // because these are closure variables it(`checks out for ${x}`, () => { diff --git a/cypress/integration/filter-spec.js b/cypress/integration/filter-spec.js index ad36708..2911497 100644 --- a/cypress/integration/filter-spec.js +++ b/cypress/integration/filter-spec.js @@ -6,9 +6,13 @@ import '../../src' describe('filter function', () => { const items = [1, 2, 3, 4] - it.each(items, (x, k) => x === 4)('only the last item matches %d', (x) => { - expect(x).to.equal(4) - }) + const n = it.each(items, (x, k) => x === 4)( + 'only the last item matches %d', + (x) => { + expect(x).to.equal(4) + expect(n, 'number of created tests').to.equal(1) + }, + ) it.each(items, (x, k) => k === 1)( 'only allows the 2nd item by index %d', diff --git a/cypress/integration/nth-spec.js b/cypress/integration/nth-spec.js index 1cc41c8..a08178d 100644 --- a/cypress/integration/nth-spec.js +++ b/cypress/integration/nth-spec.js @@ -8,14 +8,16 @@ describe('nth item', () => { context('every 2nd', () => { // take every 2nd item, same as taking the item's zero-index module 2 - it.each(items, 2)('every 2nd item %K', (x) => { + const n = it.each(items, 2)('every 2nd item %K', (x) => { expect(x, '1 or 3').to.be.oneOf([1, 3]) + expect(n, 'number of created tests').to.equal(2) }) }) context('every 3nd', () => { - it.each(items, 3)('every 3nd item %K', (x) => { + const n = it.each(items, 3)('every 3nd item %K', (x) => { expect(x, '1 or 4').to.be.oneOf([1, 4]) + expect(n, 'number of created tests').to.equal(2) }) }) }) diff --git a/cypress/integration/sample-spec.js b/cypress/integration/sample-spec.js index 608e7bd..0ea98e7 100644 --- a/cypress/integration/sample-spec.js +++ b/cypress/integration/sample-spec.js @@ -7,7 +7,11 @@ describe('_.sampleSize', () => { const items = [1, 2, 3, 4] // pick two elements from the array - it.each(Cypress._.sampleSize(items, 2))('checks %K sample %d', (x) => { - expect(x, 'one of the four').to.be.oneOf(items) - }) + const n = it.each(Cypress._.sampleSize(items, 2))( + 'checks %K sample %d', + (x) => { + expect(x, 'one of the four').to.be.oneOf(items) + expect(n, 'number of created tests').to.equal(2) + }, + ) }) diff --git a/src/index.js b/src/index.js index 1b11f1b..90daa6c 100644 --- a/src/index.js +++ b/src/index.js @@ -98,6 +98,9 @@ if (!it.each) { }) } }, this) + + // returns the number of created tests + return values.length } } } @@ -140,6 +143,9 @@ if (!describe.each) { describe(title, testCallback.bind(null, value)) } }) + + // returns the number of created suites + return values.length } } }