cypress-each
Advanced tools
Comparing version 1.12.0 to 1.13.0
{ | ||
"name": "cypress-each", | ||
"version": "1.12.0", | ||
"version": "1.13.0", | ||
"description": "Simple implementation for describe.each and it.each", | ||
@@ -11,3 +11,3 @@ "main": "src", | ||
"scripts": { | ||
"lint": "tsc --pretty --allowJs --strict --noEmit src/index.js cypress/**/*.js", | ||
"lint": "tsc --pretty --allowJs --strict --noEmit src/index.js cypress/**/*.js cypress/**/*.ts", | ||
"test": "cypress-expect run --expect cypress/expected.json", | ||
@@ -20,3 +20,3 @@ "semantic-release": "semantic-release" | ||
"devDependencies": { | ||
"cypress": "11.0.0", | ||
"cypress": "11.0.1", | ||
"cypress-expect": "2.5.3", | ||
@@ -23,0 +23,0 @@ "mocha-each": "^2.0.1", |
@@ -1,5 +0,7 @@ | ||
# cypress-each ![cypress version](https://img.shields.io/badge/cypress-9.7.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-11.0.1-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 | ||
🎓 Study the course [Cypress Plugins](https://cypress.tips/courses/cypress-plugins) | ||
## Blog posts | ||
@@ -295,2 +297,18 @@ | ||
## Test case object | ||
Sometimes you just want to have a single object that has all the tests cases together with the inputs. You can pass an object instead of an array to the `it.each` function. Each object key will become the test title, and the value will be passed to the test callback. If the value is an array, it will be destructured. See [object-input.cy.ts](./cypress/e2e/object-input.cy.ts) spec file for details. | ||
```ts | ||
const testCases = { | ||
// key: the test label | ||
// value: list of inputs for each test case | ||
'positive numbers': [1, 6, 7], // [a, b, expected result] | ||
'negative numbers': [1, -6, -5], | ||
} | ||
it.each(testCases)((a, b, expectedResult) => { | ||
expect(add(a, b)).to.equal(expectedResult) | ||
}) | ||
``` | ||
## Specs | ||
@@ -297,0 +315,0 @@ |
@@ -10,2 +10,8 @@ // types for it.each and describe.each | ||
type TestCallback<T> = (this: Context, arg0: T, arg1: any, arg2: any) => void | ||
type TestCallbackAny = ( | ||
this: Context, | ||
arg0: any, | ||
arg1: any, | ||
arg2: any, | ||
) => void | ||
@@ -27,2 +33,17 @@ interface TestFunction { | ||
): (titlePattern: string | TestTitleFn<T>, fn: TestCallback<T>) => void | ||
/** | ||
* A single test case object where the keys are test titles, | ||
* and the values are used as inputs to the test callback | ||
* @see https://github.com/bahmutov/cypress-each#test-case-object | ||
* @example | ||
* const testCases = { | ||
* // key: the test label | ||
* // value: list of inputs for each test case | ||
* 'positive numbers': [1, 6, 7], // [a, b, expected result] | ||
* 'negative numbers': [1, -6, -5], | ||
* } | ||
* it.each(testCases)((a, b, result) => { ... }) | ||
*/ | ||
each(testCases: object): (fn: TestCallbackAny) => void | ||
} | ||
@@ -29,0 +50,0 @@ |
@@ -67,4 +67,6 @@ /// <reference types="cypress" /> | ||
if (!Array.isArray(values)) { | ||
throw new Error('cypress-each: values must be an array') | ||
if (typeof totalChunks === 'number') { | ||
if (!Array.isArray(values)) { | ||
throw new Error('cypress-each: values must be an array') | ||
} | ||
} | ||
@@ -87,26 +89,53 @@ | ||
values.forEach(function (value, k) { | ||
const title = makeTitle(titlePattern, value, k, values) | ||
if (!title) { | ||
if (Cypress._.isPlainObject(values)) { | ||
testCallback = titlePattern | ||
if (typeof testCallback !== 'function') { | ||
throw new Error( | ||
`Could not compute the test title ${k} for value ${value}`, | ||
'When using a single test case object, cannot provide title pattern', | ||
) | ||
} | ||
// define a test for each value | ||
if (Array.isArray(value)) { | ||
// const title = formatTitle(testTitle, ...value) | ||
it(title, function itArrayCallback() { | ||
return testCallback.apply(this, value) | ||
}) | ||
} else { | ||
// const title = formatTitle(testTitle, value) | ||
it(title, function itCallback() { | ||
return testCallback.call(this, value) | ||
}) | ||
} | ||
}, this) | ||
const pairs = Cypress._.toPairs(values) | ||
pairs.forEach(function (pair) { | ||
const [title, value] = pair | ||
// define a test for each value | ||
if (Array.isArray(value)) { | ||
it(title, function itArrayCallback() { | ||
return testCallback.apply(this, value) | ||
}) | ||
} else { | ||
it(title, function itCallback() { | ||
return testCallback.call(this, value) | ||
}) | ||
} | ||
}, this) | ||
} else if (Array.isArray(values)) { | ||
values.forEach(function (value, k) { | ||
const title = makeTitle(titlePattern, value, k, values) | ||
if (!title) { | ||
throw new Error( | ||
`Could not compute the test title ${k} for value ${value}`, | ||
) | ||
} | ||
// returns the number of created tests | ||
return values.length | ||
// define a test for each value | ||
if (Array.isArray(value)) { | ||
it(title, function itArrayCallback() { | ||
return testCallback.apply(this, value) | ||
}) | ||
} else { | ||
it(title, function itCallback() { | ||
return testCallback.call(this, value) | ||
}) | ||
} | ||
}, this) | ||
// returns the number of created tests | ||
return values.length | ||
} else { | ||
console.error(values) | ||
throw new Error( | ||
'Do not know how to create tests from the values array / object. See DevTools console', | ||
) | ||
} | ||
} | ||
@@ -113,0 +142,0 @@ } |
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
21574
226
398