cypress-each
Advanced tools
Comparing version 1.2.2 to 1.3.0
{ | ||
"name": "cypress-each", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "Simple implementation for describe.each and it.each", | ||
@@ -5,0 +5,0 @@ "main": "src", |
@@ -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] | ||
# 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) | ||
> A demo of mocha-each and custom describe.each and it.each implementation for Cypress | ||
@@ -35,4 +35,56 @@ | ||
}) | ||
// creates tests | ||
// "element header is visible" | ||
// "element footer is visible" | ||
// "element .new-todo is visible" | ||
``` | ||
## Multiple arguments | ||
You can pass multiple arguments into the callback function by using an array of arrays. For example, to check if an element is visible, invisible, or exists, you can have both a selector and the assertion string for each item. | ||
```js | ||
const data = [ | ||
// each entry is an array [selector, assertion] | ||
['header', 'be.visible'], | ||
['footer', 'exist'] | ||
['.new-todo', 'not.be.visible'] | ||
] | ||
it.each(data)('element %s should %s', (selector, assertion) => { | ||
cy.visit('/') | ||
cy.get(selector).should(assertion) | ||
}) | ||
// creates tests | ||
// "element header should be.visible" | ||
// "element footer should exist" | ||
// "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 | ||
@@ -39,0 +91,0 @@ |
@@ -17,9 +17,11 @@ /// <reference types="cypress" /> | ||
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)) | ||
@@ -36,8 +38,10 @@ } | ||
// 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)) | ||
@@ -49,1 +53,3 @@ } | ||
} | ||
module.exports = { formatTitle } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8891
63
177