-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
docs(net-stubbing): deprecate cy.route(), remove experimentalNetworkStubbing #3323
Conversation
….route-remove-experimentalNetworkStubbing
…talNetworkStubbing # Conflicts: # source/_partial/network_stubbing_warning.md # source/guides/references/experiments.md
… for when it's breaking.
…deprecated versus breaking
…these gigantic vertical spaces)
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) |
There was a problem hiding this comment.
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)
})
})
…talNetworkStubbing
There was a problem hiding this 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()')
cy.wait('@getComments').its('response.body').should('deep.eq', JSON.stringify([ | ||
{ id: 1, comment: 'hi' }, | ||
{ id: 2, comment: 'there' } | ||
]) | ||
])) |
There was a problem hiding this comment.
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',
},
]))
cy.wait('@getUser').then(({ request }) => { | ||
expect(request.body.firstName).to.eq('Jane') |
There was a problem hiding this comment.
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')
})
})
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') | ||
}) |
There was a problem hiding this comment.
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')
})
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' | ||
}) | ||
})) |
There was a problem hiding this comment.
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',
}))
.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', |
There was a problem hiding this comment.
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',
})
})
})
Totally agree, unfortunately changing it now would be breaking change... I thought about using the |
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. |
…talNetworkStubbing
@flotwig Wouldn't 6.0 be the place to make breaking changes? |
for cypress-io/cypress#9185