cypress-each
Advanced tools
Comparing version 1.4.1 to 1.5.0
{ | ||
"name": "cypress-each", | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"description": "Simple implementation for describe.each and it.each", | ||
@@ -5,0 +5,0 @@ "main": "src", |
@@ -121,2 +121,22 @@ # 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) | ||
## Chunking | ||
There is a built-in chunking helper in `describe.each` and `it.each` to only take a subset of the items. For example, to split all items into 3 chunks, and take the middle one, use | ||
```js | ||
it.each(items, 3, 1)(...) | ||
``` | ||
The other spec files can take the other chunks. The index starts at 0, and should be less than the number of chunks. | ||
```js | ||
// split all items among 3 specs | ||
// spec-a.js | ||
it.each(items, 3, 0)(...) | ||
// spec-b.js | ||
it.each(items, 3, 1)(...) | ||
// spec-c.js | ||
it.each(items, 3, 2)(...) | ||
``` | ||
## Examples | ||
@@ -123,0 +143,0 @@ |
@@ -11,5 +11,15 @@ // types for it.each and describe.each | ||
interface TestFunction { | ||
// definition for it.each | ||
/** | ||
* Iterates over each given item (optionally chunked), and creates | ||
* a separate test for each one. | ||
* @param values Input items to create the tests form | ||
* @param totalChunks (Optional) number of chunks to split the items into | ||
* @param chunkIndex (Optional) index of the chunk to get items from | ||
* @example it.each([1, 2, 3])('test %K', (x) => ...) | ||
* @see https://github.com/bahmutov/cypress-each | ||
*/ | ||
each<T = unknown>( | ||
values: T[], | ||
totalChunks?: number, | ||
chunkIndex?: number, | ||
): (titlePattern: string | TestTitleFn<T>, fn: TestCallback<T>) => void | ||
@@ -19,7 +29,17 @@ } | ||
interface SuiteFunction { | ||
// definition for describe.each | ||
/** | ||
* Iterates over each given item (optionally chunked), and creates | ||
* a separate suite for each one. | ||
* @param values Input items to create the tests form | ||
* @param totalChunks (Optional) number of chunks to split the items into | ||
* @param chunkIndex (Optional) index of the chunk to get items from | ||
* @example describe.each([1, 2, 3])('suite %K', (item) => ...) | ||
* @see https://github.com/bahmutov/cypress-each | ||
*/ | ||
each<T = unknown>( | ||
values: T[], | ||
totalChunks?: number, | ||
chunkIndex?: number, | ||
): (titlePattern: string | TestTitleFn<T>, fn: TestCallback<T>) => void | ||
} | ||
} |
@@ -14,2 +14,21 @@ /// <reference types="cypress" /> | ||
function getChunk(values, totalChunks, chunkIndex) { | ||
// split all items into N chunks and take just a single chunk | ||
if (totalChunks < 0) { | ||
throw new Error('totalChunks must be >= 0') | ||
} | ||
if (chunkIndex < 0 || chunkIndex >= totalChunks) { | ||
throw new Error( | ||
`Invalid chunk index ${chunkIndex} vs all chunks ${totalChunks}`, | ||
) | ||
} | ||
const chunkSize = Math.ceil(values.length / totalChunks) | ||
const chunkStart = chunkIndex * chunkSize | ||
const chunkEnd = chunkStart + chunkSize | ||
const chunk = values.slice(chunkStart, chunkEnd) | ||
return chunk | ||
} | ||
function makeTitle(titlePattern, value, k, values) { | ||
@@ -31,3 +50,3 @@ if (typeof titlePattern === 'string') { | ||
if (!it.each) { | ||
it.each = function (values) { | ||
it.each = function (values, totalChunks, chunkIndex) { | ||
if (!Array.isArray(values)) { | ||
@@ -38,2 +57,7 @@ throw new Error('cypress-each: values must be an array') | ||
return function (titlePattern, testCallback) { | ||
if (typeof totalChunks === 'number' && typeof chunkIndex === 'number') { | ||
// split all items into N chunks and take just a single chunk | ||
values = getChunk(values, totalChunks, chunkIndex) | ||
} | ||
values.forEach(function (value, k) { | ||
@@ -71,2 +95,7 @@ // const testTitle = titlePattern.replace('%k', k).replace('%K', k + 1) | ||
if (typeof totalChunks === 'number' && typeof chunkIndex === 'number') { | ||
// split all items into N chunks and take just a single chunk | ||
values = getChunk(values, totalChunks, chunkIndex) | ||
} | ||
return function describeEach(titlePattern, testCallback) { | ||
@@ -96,2 +125,2 @@ // define a test for each value | ||
module.exports = { formatTitle } | ||
module.exports = { formatTitle, getChunk } |
13662
143
231