Skip to content

Commit

Permalink
feat: add support for %N formatter in the test title (#18)
Browse files Browse the repository at this point in the history
* refactor make title test

* feat: add support for %N test title
  • Loading branch information
bahmutov committed Nov 10, 2021
1 parent ccaaa0b commit 35cc247
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ it.each([10, 20, 30])('checking item %K', (x) => { ... })
// "checking item 3"
```

You can use `%N` to insert the total number of items

```js
it.each(['first', 'second'])('test %K of %N', (x) => { ... })
// creates the tests
// "test 1 of 2"
// "test 2 of 2"
```

### Title function

You can form the test title yourself using a function. The function will get the item, the index, and all items and should return a string with the test title.
Expand Down
22 changes: 21 additions & 1 deletion cypress/integration/format-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// @ts-ignore
require('../..')
// @ts-ignore
const { formatTitle } = require('../../src/index.js')
const { formatTitle, makeTitle } = require('../../src/index.js')

describe('format', () => {
const person = {
Expand Down Expand Up @@ -38,3 +38,23 @@ describe('format', () => {
},
)
})

describe('makeTitle', () => {
const values = [1, 2, 3]

it('makes title using %K', () => {
expect(makeTitle('one is %K', 1, 0, values)).to.equal('one is 1')
})

it('makes title using %k and value', () => {
expect(makeTitle('at index %k is value %d', 42, 0, values)).to.equal(
'at index 0 is value 42',
)
})

it('makes title using number of values', () => {
expect(makeTitle('value %d is %K of %N', 42, 0, values)).to.equal(
'value 42 is 1 of 3',
)
})
})
9 changes: 5 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ function getChunk(values, totalChunks, chunkIndex) {

function makeTitle(titlePattern, value, k, values) {
if (typeof titlePattern === 'string') {
const testTitle = titlePattern.replace('%k', k).replace('%K', k + 1)
const testTitle = titlePattern
.replace('%k', k)
.replace('%K', k + 1)
.replace('%N', values.length)
if (Array.isArray(value)) {
return formatTitle(testTitle, ...value)
} else {
Expand Down Expand Up @@ -66,7 +69,6 @@ if (!it.each) {
}

values.forEach(function (value, k) {
// const testTitle = titlePattern.replace('%k', k).replace('%K', k + 1)
const title = makeTitle(titlePattern, value, k, values)
if (!title) {
throw new Error(
Expand Down Expand Up @@ -113,7 +115,6 @@ if (!describe.each) {
return function describeEach(titlePattern, testCallback) {
// define a test for each value
values.forEach((value, k) => {
// const testTitle = titlePattern.replace('%k', k).replace('%K', k + 1)
const title = makeTitle(titlePattern, value, k, values)

if (!title) {
Expand All @@ -134,4 +135,4 @@ if (!describe.each) {
}
}

module.exports = { formatTitle, getChunk }
module.exports = { formatTitle, makeTitle, getChunk }

0 comments on commit 35cc247

Please sign in to comment.