cypress-io / cypress

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

Intermittent erroneous error about calling a child command before running a parent command #30522

Open jpage-godaddy opened 2 weeks ago

jpage-godaddy commented 2 weeks ago

Current behavior

I'm occasionally getting an error in my test suite. I have a beforeEach hook like this:

    beforeEach(function () {
      cy.intercept('GET', `/some/api/endpoint`, { statusCode: 200, body: [] })
        .as('getAppSettings');
    });

...and sometimes I get a failure like this:

The following error originated from your test code, not from Cypress. It was caused by an unhandled promise rejection.

> Oops, it looks like you are trying to call a child command before running a parent command.

You wrote code that looks like this:

cy.as("getAppSettings")

A child command must be chained after a parent because it operates on a previous subject.

Clearly something else is going on because I am not doing what the error says I'm doing. It seems like an unhandled exception occurring in the prior test causing an error to be misidentified. If I skip the prior test, this does not occur.

Desired behavior

Cypress should show the actual unhandled exception

Test code to reproduce

I cannot reproduce in a minimal enough way to share the full code.

Cypress Version

13.15.1

Node version

20.18.0

Operating System

Ubuntu 22.04.4 LTS

Debug Logs

I didn't see any debug output relating to this issue.

Other

I think the uncaught exception is due to an error being thrown in the browser after a previous test has completed, and that is being caught by the next test and erroneously associated with the currently executing command.

jpage-godaddy commented 2 weeks ago

Actually, this may be even simpler. It looks like in this test suite, someone erroneously made their test block an async function which I know is incorrect. Changing that fixes the issue. Maybe Cypress can detect if a Promise is returned when running a test and tell the user that they should not be using an an async function.