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

Using cy.server() and cy.route() not working in before hooks #595

Closed
pawelgalazka opened this issue Aug 17, 2017 · 5 comments
Closed

Using cy.server() and cy.route() not working in before hooks #595

pawelgalazka opened this issue Aug 17, 2017 · 5 comments
Milestone

Comments

@pawelgalazka
Copy link

pawelgalazka commented Aug 17, 2017

Current behavior:

I'm getting error in Cypress app console: CypressError: cy.route() cannot be invoked before starting the cy.server() when I use cy.server() in top before hook and then cy.route() in children contexts before hooks.

If I remove top cy.server() and move it to before hook before each cy.route() call then I get fail for second describe context with error: CypressError: The XHR server is unavailable or missing. This should never happen and likely is a bug. Open an issue if you see this message.

Desired behavior:

I should be able to use cy.route() and cy.server() within before hooks.

Test code:

describe('test', () => {
  before(() => {
    cy.server()
  })

  describe('case with http 500', () => {
    before(() => {
      // cy.server()
      cy.route({
        method: 'GET',
        url: '/api?*',
        status: 500,
        response: {}
      })
      cy.visit('http://app.dev/')
    })

    it('test n1', () => {

    })

    it('test nr 2', () => {

    })
  })

  describe('case with http 408', () => {
    before(() => {
      // cy.server()
      cy.route({
        method: 'GET',
        url: '/api?*',
        status: 408,
        response: {}
      })
      cy.visit('http://app.dev')
    })

    it('test nr 3', () => {

    })

    it('test nr 4', () => {

    })
  })
})
  • Cypress Version: 0.19.2
@jennifer-shehane
Copy link
Member

jennifer-shehane commented Aug 17, 2017

Can confirm this is a bug in 0.19.4.

First Use Case
screen shot 2017-08-17 at 10 47 38 am

Second Use Case
screen shot 2017-08-17 at 10 50 45 am

@jennifer-shehane
Copy link
Member

The second use case has been fixed in the upcoming 0.20.0 version.

The first use case still fails in the upcoming 0.20.0 version.

Minimum code to reproduce

describe('test', () => {
  before(() => {
    cy.server()
  })

  describe('describe n1', () => {
    before(() => {
      cy.route('fake')
    })

    it('test n1', () => {})
  })

  describe('describe n2', () => {
    before(() => {
      cy.route('fake')
    })

    it('test nr 2', () => {})
  })
})

@brian-mann
Copy link
Member

I looked at this, and this is not a bug, this is the correct and intended behavior of mocha hooks.

Code in a before hook only runs once and once only. When it reaches the 2nd test: test nr 2 the server has been reset and needs to be reinvoked.

Replace the notion of cy.server with anything else in the before hook - whether its another cypress command, or other javascript code. It only makes sense to run once and will not affect nor have anything to do with the execution of test nr 2.

You just need to move the before hook to beforeEach since you intend it to run before each test and not just before the first test.

@brian-mann
Copy link
Member

brian-mann commented Aug 28, 2017

To clarify I only ran @jennifer-shehane's minimal code example in our upcoming 0.20.0. I suppose something about 0.19.4 could be different, but I'm closing this issue for now. Can reopen if another demonstrable example is buggy.

@jennifer-shehane jennifer-shehane added pending release stage: wontfix Cypress does not regard this as an issue or will not implement this feature labels Aug 31, 2017
@brian-mann brian-mann added this to the 0.20.0 milestone Sep 14, 2017
@brian-mann
Copy link
Member

brian-mann commented Sep 14, 2017

Fixed the situation when calling cy.route() before cy.server() where the error would not properly print to the Command Log in 0.20.0

The initial request of using cy.server() and cy.route() in before hooks is still considered not a bug as explained here: #595 (comment)

@cypress-io cypress-io locked as resolved and limited conversation to collaborators Apr 24, 2019
@jennifer-shehane jennifer-shehane removed the stage: wontfix Cypress does not regard this as an issue or will not implement this feature label Apr 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants