Skip to content

Commit

Permalink
feat: add yieldResponseBody option
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Sep 6, 2022
1 parent 2d1ca0a commit 79fb340
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
12 changes: 12 additions & 0 deletions cypress/e2e/spec.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ describe('waitIfHappens', () => {
.should('have.length', 3)
})

it('yields the response body', () => {
cy.get('h1').should('be.visible').makeAppRequest(1000)
// you can only yield the response body using the options object
cy.waitIfHappens({
alias: '@users',
timeout: 1100,
yieldResponseBody: true,
})
// no need to have .its("response.body") chained command
.should('have.length', 3)
})

it('before the call with assertions', () => {
cy.get('h1').should('be.visible').makeAppRequest(1000)
cy.waitIfHappens('@users').its('response.body').should('have.length', 3)
Expand Down
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
Cypress.Commands.add('waitIfHappens', (_alias, _timeout) => {
let alias
let timeout
let yieldResponseBody = false

if (typeof _alias === 'object') {
// the user passed options object
alias = _alias.alias
timeout = _alias.timeout
yieldResponseBody = Boolean(_alias.yieldResponseBody)
} else {
alias = _alias
timeout = _timeout
Expand Down Expand Up @@ -37,7 +39,11 @@ Cypress.Commands.add('waitIfHappens', (_alias, _timeout) => {
if (newIntercept) {
// console.log(newIntercept)
cy.log(`**${alias}** happened after ${elapsed}ms`)
return cy.wait(alias)
if (yieldResponseBody) {
return cy.wait(alias).its('response.body', { timeout: 0 })
} else {
return cy.wait(alias)
}
}
return cy.wait(100, { log: false }).then(waitRecursive)
})
Expand Down

0 comments on commit 79fb340

Please sign in to comment.