Gathering detailed insights and metrics for cypress-wait-if-happens
Gathering detailed insights and metrics for cypress-wait-if-happens
Gathering detailed insights and metrics for cypress-wait-if-happens
Gathering detailed insights and metrics for cypress-wait-if-happens
npm install cypress-wait-if-happens
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
7 Stars
43 Commits
2 Forks
2 Watching
4 Branches
1 Contributors
Updated on 07 Sept 2024
Minified
Minified + Gzipped
JavaScript (99.09%)
HTML (0.91%)
Cumulative downloads
Total Downloads
Last day
-20.1%
2,969
Compared to previous day
Last week
-5.6%
17,087
Compared to previous week
Last month
6.2%
85,342
Compared to previous month
Last year
23.2%
1,082,219
Compared to previous year
4
A better
cy.wait
command
🎓 Covered in my Cypress Network Testing Exercises course
if the request happens, yields the intercept
if the request does not happen within the timeout, yields undefined
can yield the response body via the option yieldResponseBody: true
can yield the last intercept via the option lastCall: true
includes types in src/index.d.ts
good to use in combination with the cypress-if plugin
1import 'cypress-wait-if-happens'
2cy.waitIfHappens({
3 alias: '@users',
4 timeout: 100,
5 lastCall: true,
6 yieldResponseBody: true,
7})
8 // we should get the list with 4 users
9 // because that is the last call that happens
10 .should('have.length', 4)
Add this plugin as a dev dependency to your project
1# if using NPM 2$ npm i -D cypress-wait-if-happens 3# if using Yarn 4$ yarn add -D cypress-wait-if-happens
Import the plugin in your spec or support file
1import 'cypress-wait-if-happens'
1// timeout is in milliseconds 2// by default timeout is the current default command timeout 3cy.waitIfHappens(alias, timeout)
The above command does NOT fail if the network call is not made within timeout
ms. You can yield the intercept or undefined
to the next command
1cy.waitIfHappens(alias, timeout).then((intercept) => { 2 if (!intercept) { 3 // the call has not happened 4 } else { 5 // the intercepted call, same as 6 // the value yielded by cy.wait(alias) 7 } 8})
1cy.waitIfHappens(alias, timeout)
2// equivalent to using an options object
3cy.waitIfHappens({ alias, timeout })
Note: requires using the options object
Because yielding the response body is so common, you can yield it (if the network call happens)
1cy.waitIfHappens({
2 alias,
3 timeout,
4 yieldResponseBody: true,
5}).then((body) => {
6 if (body) {
7 // there was a network call
8 // and we intercepted it
9 } else {
10 // no network call made
11 }
12})
Note: requires using the options object
The command cy.wait
takes each network call one by one. If you are not sure how many calls there are, it is hard to say how many times to call cy.wait
. In this plugin, you can take all current calls that have happened and yield the last one
1cy.waitIfHappens({
2 alias,
3 timeout,
4 lastCall: true,
5}).then((intercept) => {
6 if (intercept) {
7 // we waited until a call was made
8 // or if there were already multiple calls
9 // we get the last intercept object
10 } else {
11 // no matching network call made at all
12 }
13})
Waiting for the last call automatically waits for all of them by calling cy.wait
repeatedly. If there are no calls made yet, waits up to the timeout
period.
This plugin works great in combination with the cypress-if plugin. See more examples in if.cy.js
1cy.waitIfHappens({ 2 alias: '@users', 3 timeout: 2000, 4 yieldResponseBody: true, 5}) 6 .if() 7 .its('length') 8 .then((n) => { 9 cy.log(`got ${n} users`) 10 })
Tested with Cypress v10 and v9
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
Copyright (c) 2022 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.
No vulnerabilities found.
No security vulnerabilities found.