cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.8k stars 3.17k forks source link

Incorrect merging behaviour when modifying response in cy.intercept #25430

Open AlexandreBonaventure opened 1 year ago

AlexandreBonaventure commented 1 year ago

Current behavior

Hello, I recently found a bug when modifying a response within listeners in cy.intercept when building a fixture system. The goal was to provide a command to progressively modify the API response.

I had a hard time figuring it all out because the response I was expecting was only partly modified. Here's the repro steps: Using multiple interceptors, provide a fixture object in each listener that should be overriding the original response and previous fixtures.

cy.intercept('https://dog.ceo/api/breeds/image/random', (req) => {
      req.on('response', (res) => {
        const result = Object.assign({}, res.body, fixture2);
        res.body = result
        // Object.assign(res.body, result) <----- works see below
      });
    });
    cy.intercept('https://dog.ceo/api/breeds/image/random', (req) => {
      req.on('response', (res) => {
        const result = Object.assign({}, res.body, fixture1);
        res.body = result
        // Object.assign(res.body, result) <----- works see below
      });
    });

When using res.body = result it only partly modify the original response. What is even more confusing is that: array: ['expected', 'expected'] was never defined in my fixture (only array: ['expected']). Likewise message should be undefined here. Otherwise, all the other string fields were correctly modified. It makes me think that there is some kind of bug in the way mutations/merge results are handled in the interceptor logic

Capture d’écran, le 2023-01-11 à 09 18 06

I eventually ended up using Object.assign as a workaround which is working as expected, weirdly enough.

Capture d’écran, le 2023-01-11 à 09 18 21

Thanks!

Desired behavior

res.body assignment res.body = result should behave the same way as Object.assign(res.body, result)

Test code to reproduce

https://github.com/AlexandreBonaventure/cypress-intercept-bug

Cypress Version

12.3.0

Node version

18.11.0

Operating System

MacOS 13.1

Debug Logs

No response

Other

No response

chrisbreiding commented 1 year ago

Thanks for reporting this bug and for the reproduction. I was able to verify the issue occurring with the repo you provided.

I'll route this to my team for further investigation.

cypress-app-bot commented 1 year ago

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

cypress-app-bot commented 1 year ago

This issue has been closed due to inactivity.

IdleThought commented 1 month ago

Is there a reason this ticket was closed? This issue is still occurring, but at least the OP's work around thankfully still works.

johnverberne commented 3 weeks ago

Thanks for the OP's work around, this still works for old cypress