-
Notifications
You must be signed in to change notification settings - Fork 3.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
cy.wait yields string instead of object as intercepted request body #14273
Comments
Im having a similar-ish issue myself. It seems the actual api response is just missing and the |
This may actually be a different issue...I've opened a new one |
@jarretmoses you issue is something else. from description sounds like the one described here https://glebbahmutov.com/blog/cypress-intercept-problems/#cached-response with solution. I have response and request body content it is just that it is string if stubbing done with |
@tlpbu This is actually only an issue with the request.body, not the response.body. Reproducible exampleit('test cy.route()', () => {
cy.server()
cy.route('POST', '/users', { foo: 'bar' }).as('postUrl')
cy.visit('https://example.com')
cy.window().then((win) => {
const xhr = new win.XMLHttpRequest()
xhr.open('POST', '/users')
xhr.send({ name: 'XYZ' })
})
cy.wait('@postUrl')
.should(xhr => {
expect(xhr.request.body).to.deep.eq({ name: 'XYZ' })
expect(xhr.response.body).to.deep.eq({ foo: 'bar' })
})
})
it('test cy.intercept()', () => {
cy.intercept('POST', '/users', { foo: 'bar' }).as('postUrl')
cy.visit('https://example.com')
cy.window().then((win) => {
const xhr = new win.XMLHttpRequest()
xhr.open('POST', '/users')
xhr.send({ name: 'XYZ' })
})
cy.wait('@postUrl')
.should(interception => {
expect(interception.response.body).to.deep.eq({ foo: 'bar' })
// ❗️ fails because request.body is a string
expect(interception.request.body).to.deep.eq({ name: 'XYZ' }
)
})
}) |
@jennifer-shehane this still fails in 6.3.0, but I think the repro is wrong, the body of the XHR is not being stringified as you expect: If I update the repro to properly send a JSON request body, it passes in 6.3.0: it('test cy.intercept()', () => {
cy.intercept('POST', '/users', { foo: 'bar' }).as('postUrl')
cy.visit('https://example.com')
cy.then((win) => {
Cypress.$.ajax({
url: '/users',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify({ name: 'XYZ' }),
})
})
cy.wait('@postUrl')
.should((interception) => {
expect(interception.response.body).to.deep.eq({ foo: 'bar' })
// ❗️ fails because request.body is a string
expect(interception.request.body).to.deep.eq({ name: 'XYZ' })
})
}) @tlpbu are you POSTing |
@flotwig Yes, it is JSON in request body. |
We're seeing a similar issue when we use cy.intercept('POST', '/users', (req) => {
// ... a bunch of logic here
req.reply((res) => {
// send an object
res.send({ name: 'XYZ' });
});
}).as('postUrl')
cy.wait('@postUrl')
.should((interception) => {
// ❗️ fails because response.body is a string
expect(interception.response.body).to.deep.eq({ name: 'XYZ' })
}) |
I have the same issue with the This works with cy.route('GET', '**/projects/*/calculation').as('projectCalculation')
cy.wait('@projectCalculation').its('response.body').should('contain.keys', ['projectId', 'name']) But this fails with cy.intercept('GET', '**/projects/*/calculation').as('projectCalculation')
cy.wait('@projectCalculation').its('response.body').should('contain.keys', ['projectId', 'name']) // etc. I also tried this: cy.wait('@projectCalculation').then(request => {
console.log(typeof request.response.body) // Yields String, the whole body is not JSON, but a String
}) |
@sybrendotinga The response type from your server is incorrect. It is text/html, but should be application/json. |
Life safer! This was my problem. |
Closing since there should not be any issues with this with the current release of Cypress as long as Content-Type is correct - see: #14273 (comment) If you're still experiencing this issue with a JSON content-type in the latest version of Cypress, please open a new issue. |
cy.wait()
yields object containing string instead of object as intercepted request/response body ifcy.intercept()
is used preventing deep equal assertions.Current behavior
If request is stubbed with
cy.route()
, thency.wait()
yields object in request and response body as expected (if json is used).Desired behavior
Yield object to simplify assertions
Test code to reproduce
I cannot provide full test due to info security but generic code involved is as:
Versions
Cypress 6.20
The text was updated successfully, but these errors were encountered: