Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document cy.state and why you'd use it #108

Open
jennifer-shehane opened this issue Sep 25, 2017 · 6 comments
Open

Document cy.state and why you'd use it #108

jennifer-shehane opened this issue Sep 25, 2017 · 6 comments
Labels
content: new section: api triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team.

Comments

@jennifer-shehane
Copy link
Member

Related to: cypress-io/cypress#467

@Threnos
Copy link

Threnos commented Mar 13, 2020

Yes, please!

@Threnos
Copy link

Threnos commented Jun 28, 2021

I use this method for checking that a specific request did not happen.
For example: test of dialog that creates user. Clicking button "Add user" starts form validation process, which highlights falsy inputs and shakes dialog box. Metod requestNotOccurred() is invoked after click on a button to test that validation works correctly and user is not created without required inputs.

type HttpMethods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'
type RequestsMap = 'userAdd' // helps with intellisense. Actually, this is a string literal union, here is a single entry for example simplicity

const requestsMap: { // holds technical information about request
  [K in RequestsMap]: {
    method: HttpMethods
    url: string
  }
} = {
  userAdd: {
    method: 'PUT',
    url: '**/User/Add'
  }
}

/**
 * Checks that no request occurred to a specified url
 * @param {RequestsMap} alias - alias of request
 * @example
 *
 * cy.get('[data-cy="users_addUserDialog_btnAddUser"]').click()
 * requestNotOccurred('userAdd')
 */
export function requestNotOccurred(alias: RequestsMap): Cypress.Chainable<null> {
  return cy.log(`Check that request "${alias}" didn't happen`).then(() => {
    for (const happenedRequest of cy.state('requests')) {
      if (
        happenedRequest.xhr.method === requestsMap[alias].method &&
        Cypress.minimatch(happenedRequest.xhr.url, requestsMap[alias].url, {
          matchBase: true
        })
      ) {
        throw new Error(`Request "${alias}" happened!`)
      }
    }
  })
}

@shwarcu
Copy link

shwarcu commented Nov 16, 2021

Hi @jennifer-shehane @bahmutov is there any plan to document this? Or at least provide typings?

@jaredasutton
Copy link

jaredasutton commented Jul 28, 2022

The Cypress website's "Test Retries" guide includes an example usage of cy.state. This inclusion warrants either a warning about its usage with TypeScript or an offering in the public API with accompanying typings.

@dwightjack
Copy link

cy.state() is also shown in the custom queries documentation, but its type is not exposed.

@nagash77 nagash77 assigned elylucas and unassigned brian-mann May 23, 2023
@nagash77 nagash77 added the triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team. label May 23, 2023
@elylucas elylucas removed their assignment Jul 31, 2023
@verheyenkoen
Copy link
Contributor

Is creating typings for cy.state within the scope of this ticket because that is also needed?

Also, is there a workaround to add it ourselves pending the release of this? I know you can add this bit, but not sure what it should return:

declare global {
  namespace Cypress {
    interface Chainable {
      state(state: string) : ???
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
content: new section: api triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants