Skip to content

Commit

Permalink
feat: add k indices to title pattern, closes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Oct 14, 2021
1 parent ca6bf6b commit 72f2f4b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,33 @@ it.each(data)('element %s should %s', (selector, assertion) => {
// "element .new-todo should not.be.visible"
```

## Test and suite titles

You can use the arguments to the test callback in the test title in order.

```js
it.each([10, 20, 30])('number is %d', (x) => { ... })
// creates the tests
// "number is 10"
// "number is 20"
// "number is 30"
```

If you want to use the iteration variable in the title, use `%k` for zero-based index, or `%K` for one-based index.

```js
it.each([10, 20, 30])('checking item %k', (x) => { ... })
// creates the tests
// "checking item 0"
// "checking item 1"
// "checking item 2"
it.each([10, 20, 30])('checking item %K', (x) => { ... })
// creates the tests
// "checking item 1"
// "checking item 2"
// "checking item 3"
```

## Examples

- Watch [Using cypress-each To Create Separate Tests](https://youtu.be/utPKRV_fL1E)
Expand Down
20 changes: 20 additions & 0 deletions cypress/integration/each-k.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @ts-check
/// <reference types="cypress" />

import '../..'

it('has test title', function () {
expect(this, 'has test').to.have.property('test')
// @ts-ignore
expect(this.test.title).to.equal('has test title')
})

it.each([1, 2, 3])('has 0-based index %k', function (x) {
expect(x).to.be.oneOf([1, 2, 3])
// needs test context to be passed correctly by it.each
// expect(this.test.title).to.equal('has 0-based index 0')
})

it.each([1, 2, 3])('has 1-based index %K', (x) => {
expect(x).to.be.oneOf([1, 2, 3])
})
9 changes: 8 additions & 1 deletion cypress/integration/format-spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// @ts-check
/// <reference types="cypress" />

import '../..'
// @ts-ignore
require('../..')
// @ts-ignore
const { formatTitle } = require('../../src/index.js')

describe('format', () => {
const person = {
name: 'Joe',
}

it('formats title using %d', () => {
expect(formatTitle('one is %d', 1)).to.equal('one is 1')
})

// it should only use the number of arguments
// in the string format, and not all available arguments
it.each([['person', person, 42]])('I am a %s', (name, who, life) => {
Expand Down
18 changes: 12 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ function formatTitle(pattern, ...values) {
if (!it.each) {
it.each = (values) => {
return function (titlePattern, testCallback) {
values.forEach((value) => {
values.forEach((value, k) => {
const testTitle = titlePattern.replace('%k', k).replace('%K', k + 1)

// define a test for each value
if (Array.isArray(value)) {
const title = formatTitle(titlePattern, ...value)
const title = formatTitle(testTitle, ...value)
it(title, testCallback.bind(null, ...value))
} else {
const title = formatTitle(titlePattern, value)
const title = formatTitle(testTitle, value)
it(title, testCallback.bind(null, value))
}
})
Expand All @@ -32,15 +34,19 @@ if (!describe.each) {
describe.each = (values) => {
return function describeEach(titlePattern, testCallback) {
// define a test for each value
values.forEach((value) => {
values.forEach((value, k) => {
const testTitle = titlePattern.replace('%k', k).replace('%K', k + 1)

if (Array.isArray(value)) {
const title = formatTitle(titlePattern, ...value)
const title = formatTitle(testTitle, ...value)
describe(title, testCallback.bind(null, ...value))
} else {
const title = formatTitle(titlePattern, value)
const title = formatTitle(testTitle, value)
describe(title, testCallback.bind(null, value))
}
})
}
}
}

module.exports = { formatTitle }

0 comments on commit 72f2f4b

Please sign in to comment.