jakedowns / CypressHelpers

cypress tips and tricks i've found
46 stars 14 forks source link

Getting uncaught exception #7

Open silroe opened 1 year ago

silroe commented 1 year ago

Hi, @jakedowns

Im getting this exception:

(uncaught exception)CypressError: Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.

The command that returned the promise was:

cy.click()

The cy command you invoked inside the promise was:

cy.openTab()

Because Cypress commands are already promise-like, you don't need to wrap them or return your own promise.

Cypress will resolve your command with whatever the final Cypress command yields.

The reason this is an error instead of a warning is because Cypress internally queues commands serially whereas Promises execute as soon as they are invoked. Attempting to reconcile this would prevent Cypress from ever resolving.

nevcos commented 10 months ago

In my case I need to test a page with a button that should open a popup.

The test clicks on the button this way:

cy.window().getById("open-popup").click();

Then the same error happens:

CypressError: Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.

The command that returned the promise was:

  > `cy.click()`

The cy command you invoked inside the promise was:

  > `cy.openTab()`

Because Cypress commands are already promise-like, you don't need to wrap them or return your own promise.

Cypress will resolve your command with whatever the final Cypress command yields.

The reason this is an error instead of a warning is because Cypress internally queues commands serially whereas Promises execute as soon as they are invoked. Attempting to reconcile this would prevent Cypress from ever resolving.
    at cy.<computed> [as openTab] (cypress_runner.js:160665:72)
    at Object.invoke (cypress_runner.js:128531:32)
    at functionStub (cypress_runner.js:131217:43)
    at Function.invoke (cypress_runner.js:130069:47)
    at agent.invoke (cypress_runner.js:144432:27)
    at windowOpen (cypress_runner.js:130329:26)

I'm setting the window.open stub in the following way:

cy.visit("/my-url", {
  onBeforeLoad(win) {
    cy.stub(win, "open", (url, target, features) => {
      cy.openTab(Cypress.config().baseUrl + url, {
        tab_name: "popup",
        window_features: features,
      });
      return true;
    }).as("windowOpen"); 
  }
});