Gathering detailed insights and metrics for cypress-map
Gathering detailed insights and metrics for cypress-map
Gathering detailed insights and metrics for cypress-map
Gathering detailed insights and metrics for cypress-map
npm install cypress-map
Typescript
Module System
Node Version
NPM Version
83.6
Supply Chain
82
Quality
86.2
Maintenance
100
Vulnerability
96.7
License
JavaScript (87.28%)
TypeScript (6.72%)
HTML (5.69%)
CSS (0.3%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
57 Stars
337 Commits
2 Forks
2 Watchers
66 Branches
1 Contributors
Updated on Jun 12, 2025
Latest Version
1.48.1
Package Id
cypress-map@1.48.1
Unpacked Size
92.28 kB
Size
21.96 kB
File Count
35
NPM Version
10.5.0
Node Version
20.19.0
Published on
May 16, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
1
Extra Cypress query commands for v12+
Add this package as a dev dependency:
$ npm i -D cypress-map
# or using Yarn
$ yarn add -D cypress-map
Include this package in your spec or support file to use all custom query commands
1import 'cypress-map'
Alternative: import only the query commands you need:
1import 'cypress-map/commands/map' 2import 'cypress-map/commands/tap' 3// and so on, see the /commands folder
1const double = (n) => n * 2 2cy.wrap(100).apply(double).should('equal', 200)
It works like cy.then
but cy.apply(fn)
is a query command. Function fn
should be synchronous, pure function that only uses the subject argument and returns new value The function callback fn
cannot use any Cypress commands cy
.
You can pass additional left arguments to the callback function. Then it puts the subject as last argument before calling the function:
1cy.wrap(8).apply(Cypress._.subtract, 4).should('equal', -4)
Without arguments, cy.applyRight
works the same as cy.apply
. If you pass arguments, then the subject plus the arguments become the arguments to the callback. The subject is at the left (first) position
1cy.wrap(8).applyRight(Cypress._.subtract, 4).should('equal', 4) 2// same as 3cy.wrap(8) 4 .apply((subject) => Cypress._.subtract(subject, 4)) 5 .should('equal', 4)
Sometimes you have the callback to apply, and you know the first argument(s), and just need to put the subject at the last position. This is where you can partially apply the known arguments to the given callback.
1// the Cypress._.add takes to arguments (a, b) 2// we know the first argument a = 5 3// so we partially apply it and wait for the subject = b argument 4cy.wrap(100).partial(Cypress._.add, 5).should('equal', 105) 5// same as 6cy.wrap(100) 7 .apply((subject) => Cypress._.add(5, subject)) 8 .should('equal', 105)
If the current subject is an array, or a jQuery object, you can apply the given callback with arguments to the first item or element. The current subject will be the last argument.
1// cy.applyToFirst(callback, ...args)
2cy.wrap(Cypress.$('<div>100</div><div>200</div>'))
3 .applyToFirst((base, el) => parseInt(el.innerText, base), 10)
4 .should('equal', 100)
If the current subject is an array, or a jQuery object, you can apply the given callback with arguments to the first item or element. The current subject will be the first argument.
1// cy.applyToFirstRight(callback, ...args)
2cy.wrap(Cypress.$('<div>100</div><div>200</div>'))
3 .applyToFirstRight((el, base) => parseInt(el.innerText, base), 10)
4 .should('equal', 100)
We often just need to call a method on the first element / item in the current subject
1cy.get(selector).invokeFirst('getBoundingClientRect') 2// compute the vertical center for example
Transforms every object in the given collection by running it through the given callback function. Can also map each object to its property. An object could be an array or a jQuery object.
1// map elements by invoking a function 2cy.wrap(['10', '20', '30']).map(Number) // [10, 20, 30] 3// map elements by a property 4cy.get('.matching') 5 .map('innerText') 6 .should('deep.equal', ['first', 'third', 'fourth'])
You can even map properties of an object by listing callbacks. For example, let's convert the age
property from a string to a number
1cy.wrap({ 2 age: '42', 3 lucky: true, 4}) 5 .map({ 6 age: Number, 7 }) 8 .should('deep.equal', { 9 age: 42, 10 lucky: true, 11 })
You can avoid any conversion to simply pick the list of properties from an object
1const person = { 2 name: 'Joe', 3 age: 21, 4 occupation: 'student', 5} 6cy.wrap(person).map(['name', 'age']).should('deep.equal', { 7 name: 'Joe', 8 age: 21, 9})
You can extract nested paths by using "." in your property path
1cy.wrap(people) 2 .map('name.first') 3 .should('deep.equal', ['Joe', 'Anna']) 4// equivalent to 5cy.wrap(people) 6 .map('name') 7 .map('first') 8 .should('deep.equal', ['Joe', 'Anna'])
If you want to pick multiple deep properties, the last segment will be the output property name
1// each person has nested objects like 2// { name: { first: '...', last: '...', }, human: { age: xx, ... } } 3cy.wrap(people).map(['name.first', 'human.age']) 4// each object will have "first" and "age" properties
1cy.get('#items li') 2 .find('.price') 3 .map('innerText') 4 .mapInvoke('replace', '$', '') 5 .mapInvoke('trim')
1cy.get('#items li') 2 .find('.price') 3 .map('innerText') 4 .mapInvoke('replace', '$', '') 5 .map(parseFloat) 6 .reduce((max, n) => (n > max ? n : max)) 7// yields the highest price
You can provide the initial accumulator value
1cy.wrap([1, 2, 3]) 2 .reduce((sum, n) => sum + n, 10) 3 .should('equal', 16)
See reduce.cy.js
1cy.get('#items li') 2 .find('.price') 3 .map('innerText') 4 .tap() // console.log by default 5 .mapInvoke('replace', '$', '') 6 .mapInvoke('trim') 7 // console.info with extra label 8 .tap(console.info, 'trimmed strings')
Notice: if the label is provided, the callback function is called with label and the subject.
A retryable query that calls the given constructor function using the new
keyword and the current subject as argument.
1cy.wrap('Jan 1, 2019') 2 // same as "new Date('Jan 1, 2019')" 3 .make(Date) 4 .invoke('getFullYear') 5 .should('equal', 2019)
A better cy.log
: yields the value, intelligently stringifies values using %
and string-format notation.
1cy.wrap(42) 2 .print() // "42" 3 // and yields the value 4 .should('equal', 42) 5// pass formatting string 6cy.wrap(42).print('the answer is %d') // "the answer is 42" 7cy.wrap({ name: 'Joe' }).print('person %o') // 'person {"name":"Joe"}' 8// use {0} with dot notation, supported deep properties 9// https://github.com/davidchambers/string-format 10cy.wrap({ name: 'Joe' }).print('person name {0.name}') // "person name Joe" 11// print the length of an array 12cy.wrap(arr).print('array length {0.length}') // "array length ..." 13// pass your own function to return formatted string 14cy.wrap(arr).print((a) => `array with ${a.length} items`) 15// if you return a non-string, it will attempt to JSON.stringify it 16cy.wrap(arr).print((list) => list[2]) // JSON.stringify(arr[2])
See print.cy.js for more examples
Finds a single item in the subject. Assumes subject is an array or a jQuery object. Uses Lodash _.find
method.
1// using predicate function 2const isThree = n => n === 3 3cy.wrap([...]).findOne(isThree).should('equal', 3) 4// using partial known properties of an object 5cy.wrap([...]).findOne({ name: 'Anna' }).should('have.property', 'name', 'Anna')
See find-one.cy.js
1cy.get('.matching') 2 .map('innerText') 3 .primo() 4 .invoke('toUpperCase') 5 .should('equal', 'FIRST')
See primo.cy.js
Works like cy.its
for objects, but gets the property for jQuery objects, which cy.its
does not
1cy.get('#items li.matching') 2 .last() 3 .prop('ariaLabel') 4 .should('equal', 'four')
See prop.cy.js
Changes a single property inside the subject by running it through the given callback function. Useful to do type conversions, for example, let's convert the "age" property to a Number
1cy.wrap({ age: '20' }) 2 .update('age', Number) 3 .should('deep.equal', { age: 20 })
Returns a DOM element from jQuery object at position k
. Returns an item from array at position k
. For negative index, counts the items from the end.
1cy.get('#items li').at(-1).its('innerText').should('equal', 'fifth')
See at.cy.js
Returns a randomly picked item or element from the current subject
1cy.get('#items li').sample().should('have.text', 'four')
If you pass a positive number, then it picks multiple elements or items
1// yields jQuery object with 3 random items 2cy.get('#items li').sample(3).should('have.length', 3)
See sample.cy.js
Yields the second element from the current subject. Could be an element or an array item.
1cy.get('#items li').second().should('have.text', 'second')
See second.cy.js
Yields the third element from the current subject. Could be an element or an array item.
1cy.get('#items li').third().should('have.text', 'third')
See third.cy.js
Saves current subject in Cypress.env
object. Note: Cypress.env object is reset before the spec run, but the changed values are passed from test to test. Thus you can easily pass a value from the first test to the second.
1it('saves value in this test', () => { 2 cy.wrap('hello, world').asEnv('greeting') 3}) 4 5it('saved value is available in this test', () => { 6 expect(Cypress.env('greeting'), 'greeting').to.equal('hello, world') 7})
Do you really want to make the tests dependent on each other?
Often we need to find a list of elements with some sub-parts. The query cy.elements
uses the parent selector plus child selectors and returns an array of arrays of strings.
Given this HTML
1<ul id="tasks"> 2 <li><span class="name">Item A</span> <span class="k">1</span></li> 3 <li><span class="name">Item B</span> <span class="k">2</span></li> 4 <li><span class="name">Item C</span> <span class="k">3</span></li> 5 <li><span class="name">Item D</span> <span class="k">4</span></li> 6</ul>
Find all items and the numbers. The parent selector is #tasks li
, and inside the parts we want to get are .k
and .name
(in this order)
1cy.elements('#tasks li', '.k', '.name').should('deep.equal', [ 2 ['1', 'Item A'], 3 ['2', 'Item B'], 4 ['3', 'Item C'], 5 ['4', 'Item D'], 6])
Queries the page using multiple selectors and returns the found elements in the specified order, no matter how they are ordered in the document. Retries if any of the selectors are not found.
1cy.getInOrder('selector1', 'selector2', 'selector3', ...) 2// yields a single jQuery subject with 3// elements for selector1 4// and selector2, 5// and selector3, etc
You can also use a single array of selector strings
1cy.getInOrder(['h1', 'h2', 'h3'])
Supports parent subject
1cy.get('...').getInOrder('...', '...')
Sometimes you just want to wait until the element is stable. For example, if the element's text content does not change for N milliseconds, then we can consider the element to be text
stable.
1cy.get('#message').stable('text') 2// yields the element
Supported types: text
, value
(for input elements), css
, and element
(compares the element reference)
You can control the quiet period (milliseconds), and pass the log
and the timeout
options
1// stable for 500ms 2// without logging 3// with maximum retries duration of 6 seconds 4cy.get('#message').stable('text', 500, { log: false, timeout: 6_000 })
When checking the CSS property to be stable, provide the name of the property:
1// retries until the CSS animation finishes 2// and the background color is red 3cy.get('#message') 4 .stable('css', 'background-color', 100) 5 // yields the element 6 .should('have.css', 'background-color', 'rgb(255, 0, 0)')
See stable.cy.js and stable-css.cy.js
experimental
Retries until the element with the given selector detaches from DOM.
1cy.contains('Click to re-render').click() 2cy.detaches('#list')
Sometimes the detachment can happen right with the action and the cy.detaches(selector)
is too late. If you know the detachment might have already happened, you need to prepare for it by using an alias stored in the Cypress.env
object:
1cy.get('#name2').asEnv('name') 2cy.contains('Click to remove Joe').click() 3cy.detaches('@name')
The jQuery object will be stored inside the Cypress.env
under the name
property.
See detach.cy.js
Computes an object/arrays of the difference with the current subject object/array.
1cy.wrap({ name: 'Joe', age: 20 }) 2 .difference({ name: 'Joe', age: 30 }) 3 .should('deep.equal', { age: { actual: 20, expected: 30 } })
You can use synchronous predicate functions to validate properties
1// confirm the value of the "age" property 2// is larger than 15 3.difference({ name: 'Joe', age: (n) => n > 15 })
Reports missing and extra properties. See difference.cy.js
Note: use have.length
to validate the number of items in an array:
1// let's check if there are 3 objects in the array 2// INSTEAD OF THIS 3.difference([Cypress._.object, Cypress._.object, Cypress._.object]) 4// USE AN ASSERTION 5.should('have.length', 3)
You can check each item in the array subject using values / predicates from the expected object.
1// list of people objects 2cy.wrap(people) 3 .difference({ 4 name: Cypress._.isString, 5 age: (age) => age > 1 && age < 100, 6 }) 7 .should('be.empty')
📝 to learn more about cy.table
command, read the blog post Test HTML Tables Using cy.table Query Command.
Extracts all cells from the current subject table. Yields a 2D array of strings.
1cy.get('table').table()
You can slice the table to yield just a region .table(x, y, w, h)
For example, you can get 2 by 2 subregion
1cy.get('table') 2 .table(0, 2, 2, 2) 3 .should('deep.equal', [ 4 ['Cary', '30'], 5 ['Joe', '28'], 6 ])
See the spec table.cy.js for more examples.
Tip: you can combine cy.table
with cy.map
, cy.mapInvoke
to get the parts of the table. For example, the same 2x2 part of the table could be extracted with:
1cy.get('table') 2 .table() 3 .invoke('slice', 2, 4) 4 .mapInvoke('slice', 0, 2) 5 .should('deep.equal', [ 6 ['Cary', '30'], 7 ['Joe', '28'], 8 ])
Tip 2: to get just the headings row, combine .table
and .its
queries
1cy.get('table') 2 .table(0, 0, 3, 1) 3 .its(0) 4 .should('deep.equal', ['Name', 'Age', 'Date (YYYY-MM-DD)'])
To get the last row, you could do:
1cy.get('table').table().invoke('slice', -1).its(0)
To get the first column joined into a single array (instead of array of 1x1 arrays)
1cy.get('table') 2 .table(0, 1, 1) // skip the heading "Name" cell 3 // combine 1x1 arrays into one array 4 .invoke('flatMap', Cypress._.identity) 5 .should('deep.equal', ['Dave', 'Cary', 'Joe', 'Anna'])
A query to convert special DOM objects into plain objects. For example, to convert DOMStringMap
instance into a plain object compatible with deep.equal
assertion we can do
1cy.get('article') 2 .should('have.prop', 'dataset') 3 .toPlainObject() 4 .should('deep.equal', { 5 columns: '3', 6 indexNumber: '12314', 7 parent: 'cars', 8 })
By default uses JSON stringify and parse back. If you want to convert using entries
and fromEntries
, add an argument:
1cy.wrap(new URLSearchParams(searchParams)).toPlainObject('entries')
In Cypress v12 cy.invoke
became a query, which made working with asynchronous methods really unwieldy. The cy.invokeOnce
is a return the old way of calling the method and yielding the resolved value.
1cy.wrap(app) 2 // app.fetchName is an asynchronous method 3 // that returns a Promise 4 .invokeOnce('fetchName') 5 .should('equal', 'My App')
See the spec invoke-once.cy.js for more examples.
Assertion
Checks the exact text match or regular expression for a single element or multiple ones
1cy.get('#name').should('read', 'Joe Smith') 2cy.get('#ages').should('read', ['20', '35', '15']) 3 4// equivalent to 5cy.get('#name').map('innerText').should('deep.equal', ['Joe Smith']) 6cy.get('#ages') 7 .map('innerText') 8 .should('deep.equal', ['20', '35', '15'])
Using with regular expression or a mix of strings and regular expressions
1cy.get('#name').should('read', /\sSmith$/) 2cy.get('#ages').should('read', [/^\d+$/, '35', '15'])
The assertion fails if the number of elements does not match the expected number of strings.
Assertion
Checks if the subject has the given property. Can check the property value. Yields the original subject
1// check if the subject has the "name" property 2cy.wrap({ name: 'Joe' }).should('possess', 'name') 3// yields the { name: 'Joe' } object 4 5// check if the subject has the "name" property with the value "Joe" 6cy.wrap({ name: 'Joe' }).should('possess', 'name', 'Joe') 7// yields the { name: 'Joe' } object
The assertion supports deep object access using the dot notation
1cy.wrap({ user: { name: 'Joe' } }).should( 2 'possess', 3 'user.name', 4 'Joe', 5)
The assertion supports arrays using [ ]
or simple dot notation
1cy.wrap({ users: [{ name: 'Joe' }, { name: 'Jane' }] }) 2 .should('possess', 'users[0].name', 'Joe') // [] notation 3 .and('possess', 'users.1.name', 'Jane') // simple dot notation
You can also pass a predicate function instead of the value to check the property value against it.
1const isDrinkingAge = (years) => years > 21 2cy.wrap({ age: 42 }).should('possess', 'age', isDrinkingAge) 3// yields the original subject
📺 You can watch this assertion explained in the video The Possess Assertion From cypress-map Plugin
Confirms the items in the current subject array are unique (using a Set
to check)
1cy.wrap([1, 2, 3]).should('be.unique') 2cy.wrap([1, 2, 2]).should('not.be.unique')
Here are a few examples to clarify the different between the cy.invoke
, cy.map
, and cy.mapInvoke
query commands, see diff.cy.js
1const list = ['apples', 'plums', 'bananas'] 2 3// cy.invoke 4cy.wrap(list) 5 // calls ".sort()" on the list 6 .invoke('sort') 7 .should('deep.equal', ['apples', 'bananas', 'plums']) 8 9// cy.mapInvoke 10cy.wrap(list) 11 // calls ".toUpperCase()" on every string in the list 12 .mapInvoke('toUpperCase') 13 .should('deep.equal', ['APPLES', 'PLUMS', 'BANANAS']) 14 15// cy.map 16const reverse = (s) => s.split('').reverse().join('') 17cy.wrap(list) 18 // reverses each string in the list 19 .map(reverse) 20 .should('deep.equal', ['selppa', 'smulp', 'sananab']) 21 // grabs the "length" property from each string 22 .map('length') 23 .should('deep.equal', [6, 5, 7])
I have added another useful command (not a query!) to this package. It allows you to process items in the array subject one by one via synchronous, asynchronous, or cy
command functions. This is because the common solution to fetch items using cy.each
, for example does not work:
1// fetch the users from a list of ids 2// 🚨 DOES NOT WORK 3cy.get(ids).each(id => cy.request('/users/' + id)).then(users => ...) 4// Nope, the yielded "users" result is ... still the "ids" subject 5// ✅ CORRECT SOLUTION 6cy.get(ids).mapChain(id => cy.request('/users/' + id)).then(users => ...)
This package includes TypeScript command definitions for its custom commands in the file commands/index.d.ts. To use it from your JavaScript specs:
1/// <reference types="cypress-map" />
If you are using TypeScript, include this module in your types list
1{ 2 "compilerOptions": { 3 "types": ["cypress", "cypress-map"] 4 } 5}
The source code is in the src/commands folder. The build command produces ES5 code that goes into the commands
folder (should not be checked into the source code control). The package.json
in its NPM distribution includes commands
plus the types from src/commands/index.d.ts
file.
should(callback)
function on the fly.Note: this module does not have filter
method because Cypress API has query commands cy.filter and cy.invoke that you can use to filter elements in a jQuery object or items in an array. See the examples in the filter.cy.js spec. 📺 See video Filter Elements And Items With Retries.
Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2022
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
No vulnerabilities found.
No security vulnerabilities found.