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

docs(net-stubbing): deprecate cy.route(), remove experimentalNetworkStubbing #3323

Conversation

flotwig
Copy link
Contributor

@flotwig flotwig commented Nov 12, 2020

for cypress-io/cypress#9185

  • in addition to marking cy.route() and cy.server() as deprecated and removing experimentalNetworkStubbing...
  • ...also updated existing documentation around network requests to refer to cy.http instead of cy.route/cy.server, where applicable
    • some older external links will need to be updated separately/kept for reference

@flotwig flotwig marked this pull request as ready for review November 16, 2020 17:43
@jennifer-shehane
Copy link
Member

Trying to make this gigantic warning on cy.route()/cy.server() more helpful and readable. This is what I got. Not done reviewing currently.

Screen Shot 2020-11-19 at 1 54 00 PM

Comment on lines -93 to +96
cy.route('POST', '/comments').as('postComment')
cy.http('POST', '/comments').as('postComment')
cy.get('.add-comment').click()
cy.wait('@postComment').then((xhr) => {
cy.exec(`echo ${JSON.stringify(xhr.responseBody)} >cypress/fixtures/comment.json`)
cy.fixture('comment.json').should('deep.eq', xhr.responseBody)
cy.wait('@postComment').then(({ response }) => {
cy.exec(`echo ${JSON.stringify(response.body)} >cypress/fixtures/comment.json`)
cy.fixture('comment.json').should('deep.eq', response.body)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example doesn't work, so is not the equivalent of the previous version. The assertion seems to fail now because there are newlines in the body. (I'm still reviewing but this is one example of a change needed that I've run into)

  it('cy.route() - passes', () => {
    cy.visit('https://example.cypress.io/commands/network-requests')
    cy.server()
    cy.route('POST', '/comments').as('postComment')
    cy.get('.network-post').click()
    cy.wait('@postComment').then((xhr) => {
      cy.exec(`echo ${JSON.stringify(xhr.responseBody)} >cypress/fixtures/comment1.json`)
      cy.fixture('comment.json').should('deep.eq', xhr.responseBody)
    })
  })

  it('cy.http() - fails', () => {
    cy.visit('https://example.cypress.io/commands/network-requests')
    cy.http('POST', '**/comments').as('postComment')
    cy.get('.network-post').click()
    cy.wait('@postComment').then(({ response }) => {
      cy.exec(`echo ${JSON.stringify(response.body)} >cypress/fixtures/comment2.json`)
      cy.fixture('comment.json').should('deep.eq', response.body)
    })
  })

Screen Shot 2020-11-19 at 8 17 43 PM

Copy link
Member

@jennifer-shehane jennifer-shehane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found some examples that don't seem to work for me, primarily when trying to assert off of the request / response.

A comment on the feature in general: I don't like that you have to use JSON.parse() to make assertions on the response body. I think this is a poorer experience than how you could assert of cy.route() and will trip up a lot of people when they move over.

// was
expect(xhr.responseBody).to.have.property('name', 'Using POST in cy.route()')

// now
expect(JSON.parse(String(response.body))).to.have.property('name', 'Using POST in cy.http()')

Comment on lines +208 to +211
cy.wait('@getComments').its('response.body').should('deep.eq', JSON.stringify([
{ id: 1, comment: 'hi' },
{ id: 2, comment: 'there' }
])
]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example also doesn't work (same as as.md) due to newlines it looks like.

it('cy.http() - route responses to matching requests', () => {
  cy.visit('https://example.cypress.io/commands/network-requests')
  cy.http('GET', '**/comments/*').as('getComment')
  cy.get('.network-btn').click()
  cy.wait('@getComment').its('response.body').should('deep.eq', JSON.stringify([
    {
      postId: 1,
      id: 1,
      name: 'id labore ex et quam laborum',
      email: '[email protected]',
      body: 'laudantium enim quasi est quidem magnam voluptate ipsam eos\ntempora quo necessitatibus\ndolor quam autem quasi\nreiciendis et nam sapiente accusantium',
    },
  ]))

Comment on lines +161 to +162
cy.wait('@getUser').then(({ request }) => {
expect(request.body.firstName).to.eq('Jane')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns undefined, when trying to drill into the request.body.prop because body is a string.

  it('cy.http() - route responses to matching requests', () => {
    cy.visit('https://example.cypress.io/commands/network-requests')
    cy.http('GET', '**/comments/*').as('getComment')
    cy.get('.network-btn').click()
    cy.wait('@getComment').then(({ response }) => {
      expect(response.body.name).to.eq('id labore ex et quam laborum')
    })
  })

Screen Shot 2020-11-20 at 1 57 39 PM

Comment on lines +422 to +428
cy.http('POST', '/users', { id: 123 }).as('postUser')

cy.get('form').submit()

cy.wait('@postUser').its('requestBody').should('have.property', 'name', 'Brian')
cy.wait('@postUser').then(({ request }) => {
expect(JSON.parse(request.body)).to.have.property('name', 'Brian')
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example doesn't seem to work either.

it('cy.http() - route responses to matching requests', () => {
  cy.visit('https://example.cypress.io/commands/network-requests')
  cy.http('POST', '/comments', { id: 123 }).as('postComment')
  cy.get('.network-post').click()
  cy.wait('@postComment').then(({ request }) => {
    expect(JSON.parse(request.body)).to.have.property('name', 'Brian')
  })

Screen Shot 2020-11-20 at 2 13 24 PM

Comment on lines +318 to +331
cy.http('POST', '/users').as('new-user')
// trigger network calls by manipulating web app's user interface, then
cy.wait('@new-user')
.should('have.property', 'status', 201)
.should('have.property', 'response.statusCode', 201)

// we can grab the completed XHR object again to run more assertions
// we can grab the completed request object again to run more assertions
// using cy.get(<alias>)
cy.get('@new-user') // yields the same XHR object
.its('requestBody') // alternative: its('request.body')
.should('deep.equal', {
cy.get('@new-user') // yields the same request object
.its('request.body')
.should('deep.equal', JSON.stringify({
id: '101',
firstName: 'Joe',
lastName: 'Black'
})
}))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example doesn't work either.

it('cy.http() - route responses to matching requests', () => {
  cy.visit('https://example.cypress.io/commands/network-requests')

  cy.http('POST', '/comments').as('postComment')
  cy.get('.network-post').click()

  cy.wait('@postComment')
    .should('have.property', 'response.statusCode', 200)

  cy.get('@postComment') // yields the same request object
    .its('request.body')
    .should('deep.equal', JSON.stringify({
      id: '101',
      firstName: 'Joe',
      lastName: 'Black',
    }))

Screen Shot 2020-11-20 at 2 30 47 PM

Comment on lines +335 to 343
.should(({ request, response }) => {
expect(request.url).to.match(/\/users$/)
expect(request.method).to.equal('POST')
// it is a good practice to add assertion messages
// as the 2nd argument to expect()
expect(xhr.response.headers, 'response headers').to.include({
expect(response.headers, 'response headers').to.include({
'cache-control': 'no-cache',
expires: '-1',
'content-type': 'application/json; charset=utf-8',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe there's something wrong with my examples? This doesn't work for me either.

  it('cy.http() - route responses to matching requests', () => {
    cy.visit('https://example.cypress.io/commands/network-requests')

    cy.http('POST', '/comments').as('postComment')
    cy.get('.network-post').click()

    cy.wait('@postComment')

    cy.get('@postComment')
    .should(({ request, response }) => {
      expect(request.url).to.match(/\/users$/)
      expect(request.method).to.equal('POST')
      // it is a good practice to add assertion messages
      // as the 2nd argument to expect()
      expect(response.headers, 'response headers').to.include({
        'cache-control': 'no-cache',
        expires: '-1',
        'content-type': 'application/json; charset=utf-8',
      })
    })
})

Screen Shot 2020-11-20 at 2 33 17 PM

@flotwig
Copy link
Contributor Author

flotwig commented Nov 20, 2020

A comment on the feature in general: I don't like that you have to use JSON.parse() to make assertions on the response body. I think this is a poorer experience than how you could assert of cy.route() and will trip up a lot of people when they move over.

Totally agree, unfortunately changing it now would be breaking change... I thought about using the content-type to automatically parse JSON, for example.

@jennifer-shehane
Copy link
Member

I'm going to merge this in because the conflicts in 6.0 branch are getting out of hand for me, but these examples still need addressing.

@jennifer-shehane jennifer-shehane merged commit 75a10d9 into 6.0.0-release Nov 23, 2020
@jennifer-shehane
Copy link
Member

@flotwig Wouldn't 6.0 be the place to make breaking changes?

@jennifer-shehane jennifer-shehane mentioned this pull request Nov 23, 2020
11 tasks
@matthamil matthamil deleted the deprecate-cy.route-remove-experimentalNetworkStubbing branch April 14, 2021 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants