Skip to content

Missing exception when using cy.log() in Promise: Instead return value is modified #3536

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

Closed
gabbersepp opened this issue Feb 20, 2019 · 2 comments

Comments

@gabbersepp
Copy link
Contributor

gabbersepp commented Feb 20, 2019

Current behavior:

When cy.xx is called inside a Promise that returns something, no exception is thrown. Instead, the return value is modified to be the return value of the cy.xxx call.

Desired behavior:

I know that we are not allowed to call cy.xxx commands within promises. But it is very confusing to modify the return value instead throwing an exception.
In our case we used cy.log() and were wondering why we receive "undefined".
I would expect an exception to be thrown.

Steps to reproduce:

 function makeSomething(log: boolean): void {
  cy
    .then(() => Promise.resolve().then(() => returnTrue(log)))
    .then((val: boolean) => cy.log(`val is: ${val}`))
    .wait(5000);
}

function returnTrue(log: boolean): boolean {
  if (log) {
    cy.log("Test");
  }

  return true;
}

describe("Test", () => {
  it("test with log", () => {
    makeSomething(true);
  });

  it("test without log", () => {
    makeSomething(false);
  });
});

Output:

test with log
LOG val is: null
test without log
LOG val is: true

Versions

Cypress 3.1.5
Windows 10
Chrome 72.0.3626.109 (Official Build) (64-Bit)

@cypress-bot cypress-bot bot added the stage: needs investigating Someone from Cypress needs to look at this label Nov 6, 2019
@jennifer-shehane jennifer-shehane added the type: unexpected behavior User expected result, but got another label Nov 6, 2019
@BiosBoy
Copy link

BiosBoy commented Apr 27, 2020

Same here. Any updates?

@jennifer-shehane
Copy link
Member

This is because cy.log() yields null which is what the Promise resolves to. If you changed the cy.log() line to cy.window() - you would see that the yield of the Cypress command (<window>) is being passed on to the next .then().

And yes, this is why we advise against running cypress commands within Promises since commands themselves are promises and it's confusing if you mix Promise chains within Promise chains.

We try to throw a warning in these cases, but I suppose this is an instance where we don't.

Simplified version of what is happening.

it("test with log", () => {
  cy.then(() => {
      return Promise.resolve().then(() => {
        // THIS LINE IS DIFFERENT
        cy.log("Test") // enqueued, then runs

        return true
      })
    })
    .then((val) => {
      expect(val).to.be.true
    })
})

it("test without log", () => {
  cy.then(() => {
      return Promise.resolve().then(() => {
        return true
      })
    })
    .then((val) => {
      expect(val).to.be.true
    })
});

@jennifer-shehane jennifer-shehane removed stage: needs investigating Someone from Cypress needs to look at this type: unexpected behavior User expected result, but got another labels May 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants