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

cy.origin() causes async issue with GET request (JSON parsing error) -> Unexpected end of JSON input #31195

Open
alecmestroni opened this issue Feb 28, 2025 · 4 comments
Assignees
Labels
topic: cy.origin Problems or enhancements related to cy.origin command type: bug

Comments

@alecmestroni
Copy link

alecmestroni commented Feb 28, 2025

Current behavior

When visiting a page using cy.visit() inside cy.origin(), a subsequent GET request to download a JSON file causes a problem. The request is asynchronous, and Cypress loses sync inside cy.origin(). As a result, Cypress tries to parse the response (JSON) before it has been fully downloaded, causing a JSON parsing error.
Visiting the page outside of the cy.origin causes no problem.

Actual behavior: The JSON file is not fully downloaded before the client attempts to parse it, causing a parsing error.

Desired behavior

Expected behavior: Cypress should wait for the GET request to finish downloading the JSON file before attempting to parse it.

Test code to reproduce

describe("Testing Cross Origin", () => {
  it("test", () => {
    cy.visit('www.google.com');
    cy.origin('https://demoportal-stage.infocert-labs.eu', () => {
      cy.visit('https://demoportal-stage.infocert-labs.eu/login')
    })
  });
});

Cypress Version

13.17.0 or 14.1.0

Node version

v20.18.3

Operating System

Edition Windows 11 Enterprise Version 24H2 Installed on ‎20/‎01/‎2025 OS build 26100.3194 Experience Windows Feature Experience Pack 1000.26100.48.0

@FrancescoKrx
Copy link

Same problem here!

@BomberAlex
Copy link

Hy, i have the same issue with the new Cypress 14 cy.origin
Hope it's going to be fixed soon

@jennifer-shehane jennifer-shehane added topic: cy.origin Problems or enhancements related to cy.origin command type: bug labels Feb 28, 2025
@cacieprins
Copy link
Contributor

cacieprins commented Feb 28, 2025

Hi everyone, I'm sorry you're having these issues!

In order to support cy.origin(), Cypress patches XMLHttpRequest in the iframe that your application is loaded in. In this patch, XMLHttpRequest.prototype.send is modified to always be asynchronous.

Unfortunately, this causes an issue if you open() the XMLHttpRequest with false as the third parameter, flagging that request as synchronous: open() will return immediately, and if you do not ensure the request's readyState property is 4 ("Done") before parsing the request, errors can occur.

Synchronous XMLHttpRequests have been deprecated for some time because they cause hangs with poor network conditions.

@alecmestroni , can you update your code to use an asynchronous XMLHttpRequest? The context for the synchronous XMLHttpRequest in the reproduction case you provided is in the main-$HASH.js asset (this is bundled and minified, so it may take some effort to track down in your codebase):

var dp = null
  /* snip */
 , V0 = new XMLHttpRequest;
V0.open("GET", l4, !1);
V0.send(null);
dp = JSON.parse(V0.responseText);

If this is coming from a third party dependency, we recommend filing an issue with them to fix the use of a deprecated API.

@cacieprins cacieprins self-assigned this Feb 28, 2025
@alecmestroni
Copy link
Author

@cacieprins thanks for the answer.
Since this is coming from a third-party dependency, I don’t have direct control over the code causing the issue. However, I would like to know if there is any way I can address or work around this problem on my side. For example, is there a way to configure Cypress or the test environment to allow for synchronous requests, or any other potential solutions I can implement to mitigate this? (in addition to the issue)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: cy.origin Problems or enhancements related to cy.origin command type: bug
Projects
None yet
Development

No branches or pull requests

5 participants