Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
cypress-each
Advanced tools
A demo of mocha-each and custom describe.each and it.each implementation for Cypress
# install using NPM
$ npm i -D cypress-each
# install using Yarn
# yarn add -D cypress-each
Import cypress-each
in a single spec or in Cypress support file
import 'cypress-each'
// now can use describe.each and it.each
Let's create a separate test for each selector from a list
import 'cypress-each'
// create a separate test for each selector
const selectors = ['header', 'footer', '.new-todo']
it.each(selectors)('element %s is visible', (selector) => {
cy.visit('/')
cy.get(selector).should('be.visible')
})
// creates tests
// "element header is visible"
// "element footer is visible"
// "element .new-todo is visible"
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.
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"
You can use the arguments to the test callback in the test title in order.
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.
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"
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.
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
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
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
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.
// 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)(...)
Find the implementation in src/index.js
it.each
helper to generate multiple it
tests given a data arraydescribe.each
helper to create describe
blocks for each item in the given data arrayit
tests for each data itemThis package includes TypeScript definition for it.each
and describe.each
. Thus the parameter should be the right type from the array of values:
it.each([
{ name: 'Joe', age: 30 },
{ name: 'Mary', age: 20 },
])('has correct types', (user) => {
// the type for the "user" should be
// name: string, age: number
expect(user).to.have.keys('name', 'age')
expect(user.name).to.be.a('string')
expect(user.age).to.be.a('number')
})
Include this module with other library types, like
{
"compilerOptions": {
"types": ["cypress", "cypress-each"],
}
}
Or inside an individual spec file add
/// <reference types="cypress-each" />
Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2021
License: MIT - do anything with the code, but don't blame me if it does not work.
Support: if you find any problems with this module, email / tweet / open issue on Github
Copyright (c) 2021 Gleb Bahmutov <gleb.bahmutov@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Simple implementation for describe.each and it.each
The npm package cypress-each receives a total of 49,763 weekly downloads. As such, cypress-each popularity was classified as popular.
We found that cypress-each demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.