Skip to content

Commit

Permalink
feat: add return with the number of created tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Nov 21, 2021
1 parent 80478fd commit 86d590f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion cypress/integration/describe-each-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`, () => {
Expand Down
10 changes: 7 additions & 3 deletions cypress/integration/filter-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 4 additions & 2 deletions cypress/integration/nth-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
10 changes: 7 additions & 3 deletions cypress/integration/sample-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
)
})
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ if (!it.each) {
})
}
}, this)

// returns the number of created tests
return values.length
}
}
}
Expand Down Expand Up @@ -140,6 +143,9 @@ if (!describe.each) {
describe(title, testCallback.bind(null, value))
}
})

// returns the number of created suites
return values.length
}
}
}
Expand Down

0 comments on commit 86d590f

Please sign in to comment.