Search code examples
javascriptautomationcypressalias

Why am I getting a "could not find a registered alias" error?


I am getting this error: cy.wait() could not find a registered alias for: @waiting. You have not aliased anything yet

describe('', () => {
  it('sign up page', () => {
    cy.visit('/#/register')
  });
  const {userName, email, password} = generateNum()
  it('username', function() {
    cy.get('[placeholder="Username"]').type(userName).then(response => ({...userName}))
  })
  it('email', () => {
    cy.get('[placeholder="Email"]').type(email).then(response => ({...email}))
  })
    **it('intercept_w8', () => {
      cy.intercept('POST', '/api/users/').as('waiting')**
    });
  it('password', () => {
    cy.get('[placeholder="Password"]').type(password).then(response => ({...password}))
    cy.get('.btn').should('contain.text', 'Sign in').click()
  })
    **it('wait', () => {
      cy.wait('@waiting')**
    });
  it('registered user is loged in', () => {
    cy.visit('/#/login')
  });
  it('RegisteredEmail', () => {
    cy.get('[placeholder="Email"]').type(email)
  });
  it('RegisteredPassword', () => {
    cy.get('[placeholder="Password"]').type(password+'{enter}')
  });
})

So cypress is starting to use the it'registered user is loged in' before the registration is finished. So I'm trying to use the cy.intercept before click() and after wait() to wait the registration and have this error:

cy.wait() could not find a registered alias for: @waiting.

This is were the click goes:

click
(xhr)POST --- /api/users

Solution

  • Cypress clears the alias between tests.

    Aliases are reset before each test

    Note: all aliases are reset before each test. A common user mistake is to create aliases using the before hook. Such aliases work in the first test only!

    Remove the (unnecessary) it() blocks. Why do you have them there?