From 35cc247dfc06fc4628c4aeaf513f8de7d4282c16 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Wed, 10 Nov 2021 11:42:29 -0500 Subject: [PATCH] feat: add support for %N formatter in the test title (#18) * refactor make title test * feat: add support for %N test title --- README.md | 9 +++++++++ cypress/integration/format-spec.js | 22 +++++++++++++++++++++- src/index.js | 9 +++++---- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1b32e7e..84af5e6 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/cypress/integration/format-spec.js b/cypress/integration/format-spec.js index 98731d9..6551d5f 100644 --- a/cypress/integration/format-spec.js +++ b/cypress/integration/format-spec.js @@ -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 = { @@ -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', + ) + }) +}) diff --git a/src/index.js b/src/index.js index ba7346f..c06c522 100644 --- a/src/index.js +++ b/src/index.js @@ -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 { @@ -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( @@ -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) { @@ -134,4 +135,4 @@ if (!describe.each) { } } -module.exports = { formatTitle, getChunk } +module.exports = { formatTitle, makeTitle, getChunk }