cypress-each
Advanced tools
Comparing version 1.3.1 to 1.4.0
{ | ||
"name": "cypress-each", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "Simple implementation for describe.each and it.each", | ||
@@ -19,7 +19,7 @@ "main": "src", | ||
"devDependencies": { | ||
"cypress": "^8.5.0", | ||
"cypress-expect": "^2.4.3", | ||
"cypress": "8.6.0", | ||
"cypress-expect": "2.4.3", | ||
"mocha-each": "^2.0.1", | ||
"prettier": "^2.4.1", | ||
"semantic-release": "^18.0.0", | ||
"semantic-release": "18.0.0", | ||
"typescript": "^4.4.3" | ||
@@ -26,0 +26,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
# cypress-each ![cypress version](https://img.shields.io/badge/cypress-8.5.0-brightgreen) [![renovate-app badge][renovate-badge]][renovate-app] [![ci](https://github.com/bahmutov/cypress-each/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/bahmutov/cypress-each/actions/workflows/ci.yml) | ||
# cypress-each ![cypress version](https://img.shields.io/badge/cypress-8.6.0-brightgreen) [![renovate-app badge][renovate-badge]][renovate-app] [![ci](https://github.com/bahmutov/cypress-each/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/bahmutov/cypress-each/actions/workflows/ci.yml) | ||
> A demo of mocha-each and custom describe.each and it.each implementation for Cypress | ||
@@ -89,2 +89,33 @@ | ||
### 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. | ||
```js | ||
function makeTestTitle(s, k, strings) { | ||
return `test ${k + 1} for "${s}"` | ||
} | ||
it.each(['first', 'second'])(makeTestTitle, () => ...) | ||
// creates the tests | ||
// 'test 1 for "first"' | ||
// 'test 2 for "second"' | ||
``` | ||
It is very useful for forming a test title based on a property of an object, like | ||
```js | ||
it.each([ | ||
{ name: 'Joe', age: 30 }, | ||
{ name: 'Mary', age: 20 }, | ||
])( | ||
(person) => `tests person ${person.name}`, | ||
(person) => { ... } | ||
}) | ||
// creates the tests | ||
// "tests person Joe" | ||
// "tests person Mary" | ||
``` | ||
See [cypress/integration/title-function.js](./cypress/integration/ title-function.js) for more examples | ||
## Examples | ||
@@ -91,0 +122,0 @@ |
// types for it.each and describe.each | ||
// any help improving them is welcome | ||
// https://github.com/bahmutov/cypress-each | ||
type TestTitleFn<T> = (item: T, index: number, items: T[]) => string | ||
declare namespace Mocha { | ||
@@ -11,3 +14,3 @@ type TestCallback<T> = (this: Context, arg0: T, arg1: any, arg2: any) => void | ||
values: T[], | ||
): (titlePattern: string, fn: TestCallback<T>) => void | ||
): (titlePattern: string | TestTitleFn<T>, fn: TestCallback<T>) => void | ||
} | ||
@@ -19,4 +22,4 @@ | ||
values: T[], | ||
): (titlePattern: string, fn: TestCallback<T>) => void | ||
): (titlePattern: string | TestTitleFn<T>, fn: TestCallback<T>) => void | ||
} | ||
} |
@@ -14,2 +14,17 @@ /// <reference types="cypress" /> | ||
function makeTitle(titlePattern, value, k, values) { | ||
if (typeof titlePattern === 'string') { | ||
const testTitle = titlePattern.replace('%k', k).replace('%K', k + 1) | ||
if (Array.isArray(value)) { | ||
return formatTitle(testTitle, ...value) | ||
} else { | ||
return formatTitle(testTitle, value) | ||
} | ||
} else if (typeof titlePattern === 'function') { | ||
return titlePattern(value, k, values) | ||
} | ||
throw new Error('titlePattern must be a string or function') | ||
} | ||
if (!it.each) { | ||
@@ -19,7 +34,13 @@ it.each = function (values) { | ||
values.forEach(function (value, k) { | ||
const testTitle = titlePattern.replace('%k', k).replace('%K', k + 1) | ||
// const testTitle = titlePattern.replace('%k', k).replace('%K', k + 1) | ||
const title = makeTitle(titlePattern, value, k, values) | ||
if (!title) { | ||
throw new Error( | ||
`Could not compute the test title ${k} for value ${value}`, | ||
) | ||
} | ||
// define a test for each value | ||
if (Array.isArray(value)) { | ||
const title = formatTitle(testTitle, ...value) | ||
// const title = formatTitle(testTitle, ...value) | ||
it(title, function itArrayCallback() { | ||
@@ -29,3 +50,3 @@ return testCallback.apply(this, value) | ||
} else { | ||
const title = formatTitle(testTitle, value) | ||
// const title = formatTitle(testTitle, value) | ||
it(title, function itCallback() { | ||
@@ -45,9 +66,16 @@ return testCallback.call(this, value) | ||
values.forEach((value, k) => { | ||
const testTitle = titlePattern.replace('%k', k).replace('%K', k + 1) | ||
// const testTitle = titlePattern.replace('%k', k).replace('%K', k + 1) | ||
const title = makeTitle(titlePattern, value, k, values) | ||
if (!title) { | ||
throw new Error( | ||
`Could not compute the suite title ${k} for value ${value}`, | ||
) | ||
} | ||
if (Array.isArray(value)) { | ||
const title = formatTitle(testTitle, ...value) | ||
// const title = formatTitle(testTitle, ...value) | ||
describe(title, testCallback.bind(null, ...value)) | ||
} else { | ||
const title = formatTitle(testTitle, value) | ||
// const title = formatTitle(testTitle, value) | ||
describe(title, testCallback.bind(null, value)) | ||
@@ -54,0 +82,0 @@ } |
10837
93
208