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

Return Promise issues #2979

Closed
Shila7 opened this issue Dec 21, 2018 · 19 comments
Closed

Return Promise issues #2979

Shila7 opened this issue Dec 21, 2018 · 19 comments
Labels
pkg/driver This is due to an issue in the packages/driver directory

Comments

@Shila7
Copy link

Shila7 commented Dec 21, 2018

promiseerror
promisecode
@jennifer-shehane How can I solve this issues?

@Dan195
Copy link

Dan195 commented Dec 23, 2018

I would like to add I am having similar, inconsistent, behavior with this error. In my scenario, this error only occurs when running all the tests together, and so far has only occurred when the previous test case results in an error that's being addressed in this thread.

Current Behaivor:

Occasionally, when running all tests, I will get the following error

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.visit()

The cy command you invoked inside the promise was:

  > cy.viewport()

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.

https://on.cypress.io/returning-promise-and-commands-in-another-command

Because this error occurred during a 'before all' hook we are skipping the remaining tests in the current suite: 'Register an unregistered th...'

In my before hook for this test case, I have the following code.

before(() => {
  cy.viewport('macbook-15');
  cy.visit(Routes().dashboardBaseUrl);
  cy.get('input[name=email]').type(Accounts().activeIncidentEmail);
  cy.get('input[name=password]')
    .type(Accounts().activeIncidentPw)
    .type('{enter}');
});

I have not overwritten visit nor viewport anywhere in any test case. This never happens when running a single test case by itself, but happens somewhat infrequently when running all tests.

@jennifer-shehane
Copy link
Member

@Dan195 Are you using any custom commands anywhere within your test suite?

@jennifer-shehane jennifer-shehane added stage: investigating Someone from Cypress is looking into this pkg/driver This is due to an issue in the packages/driver directory labels Jan 29, 2019
@danhaasalert
Copy link

danhaasalert commented Jan 29, 2019 via email

@jennifer-shehane
Copy link
Member

Could you provide the code within your custom commands?

@danhaasalert
Copy link

danhaasalert commented Jan 30, 2019 via email

@jennifer-shehane
Copy link
Member

Yeah, I'm not seeing anything from the examples provided that could be causing the error you provided unfortunately. You'd likely have to simplify the code some to find the smallest reproducible example that causes the error.

@jennifer-shehane jennifer-shehane added stage: needs information Not enough info to reproduce the issue and removed stage: investigating Someone from Cypress is looking into this labels Jan 30, 2019
@jennifer-shehane
Copy link
Member

Unfortunately we have to close this issue as there is not enough information to reproduce the problem.

Please comment in this issue with a reproducible example and we will reopen the issue. 🙏

@jennifer-shehane jennifer-shehane removed the stage: needs information Not enough info to reproduce the issue label Apr 25, 2019
@wataruoguchi
Copy link

wataruoguchi commented May 17, 2019

I've got the same error.

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.wait()

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

The part getting the error in the test look like:

cy.someCustomCommand(arg);
cy.get('.modal button.btn').click();

In the script in support is:

Cypress.Commands.add('someCustomCommand', function(arg) {
  cy.window().should( (win) => {
    var someObject = win.someWindowObject;
    expect(someObject).to.not.be.undefined;
  }).then(() => {
    cy.log("Arg: " + arg);
  });
}

We see this error sometimes but not always. I hope it's helpful for debugging the issue.

@jennifer-shehane
Copy link
Member

@wataruoguchi There must be some information missing from what is provided above. The error indicates the cy.click() is invoked inside a cy.wait() - can you paste where the cy.wait() is defined?

@wataruoguchi
Copy link

@jennifer-shehane Oddly, I don't see any cy.wait() in the test code 🤔

@CJ-Lab7
Copy link

CJ-Lab7 commented Jun 3, 2019

I've been seeing the same error. It always happens on the same tests, but only when using the test runner (not headless mode). Also these tests were passing a couple of weeks ago.

Here's an example of the error:
Screen Shot 2019-06-03 at 11 07 15 AM

@jennifer-shehane
Copy link
Member

@CJ-Lab7 This error looks to be coming from your own application code. In the error, you can see it is coming from (http://127.0.0.1:32792/js/lims.50c0b27d.js:1) - this is your application's file. Click on the error with DevTools open to see more information about the error.

@elm9
Copy link

elm9 commented Jun 18, 2019

I just merged some unrelated changes into a new test branch, ran npm i, and the cy.visit() function began throwing this error. I find it odd because I haven't done anything regarding the visit command that is built into cypress. I even looked in the cypress repo to make sure there isn't a wait function inside . Is anyone else having this issue?

Screen Shot 2019-06-18 at 12 29 11 PM

Here is the piece of code that is throwing this error:
Screen Shot 2019-06-18 at 12 39 30 PM

@jennifer-shehane
Copy link
Member

Unfortunately there is still not enough information to reproduce the problem.

Please comment in this issue with a reproducible example and we will reopen the issue. 🙏

@TimChebotarev
Copy link

TimChebotarev commented Jul 3, 2019

I got the same error on the first type() command

describe('C T', () => {
  before(() => {
    cy.on('uncaught:exception', (err, runnable) => {
      if (err.match(/Uncaught Error: ResizeObserver loop limit exceeded/))
        return false
    })
  })
  beforeEach(() => {
    cy.server()
    cy.fixture('c/all.json').as('fxAllC')
    cy.route('**/T**').as('data')
    cy.route('GET',
      '**/C/T?dateFrom=**',
      '@fxAllC').as('fullData')

    cy.route('POST', '**/Search').as('search')

    cy.login()
    cy.visit('/c')
    cy.wait('@fullData', { timeout: 20000, })
  })
  it('Text search', () => {
    cy.contains('h1', 'Count')

    cy.get('.ct-search .aff input').type('leader')
    cy.wait('@search')
    cy.get('.aff-item').first().click()

    cy.get('.ct-search .aff input').type('11111111111111')
    cy.wait('@search')
    cy.get('.aff-item').should('not.exist')

    cy.get('.ct-search .aff input').type('{enter}')

    cy.get('.aff .aff-selected-item').should((items) => {
      expect(items).to.have.length(2)
      expect(items.eq(1)).to.contain('Leader 1')
      expect(items.eq(0)).to.contain('11111111111111')
    })
  })
})

image

Also I have commands

Cypress.Commands.add('app', () => {
  cy.window().should('have.property', '$app')
  return cy.window().its('$app')
})

Cypress.Commands.add('login', () => {
  return api.login('User', '123456')
})

@oskargustafsson
Copy link

I see a similar issue, which occurs randomly when running a certain test:

// all(...) creates it(...) permutations with different viewports and languages
all( 
  `${testProduct.name} can be found through navigation, sorting and filtering`,
  topCategoryPageUrl,
  ({ viewport, t }) => {
    assertBreadcrumbs(2, viewport);

    cy.get(`header h1`).should('exist');
		
    // Navigate to sub-category
    cy.get(el('CategoryList'))
      .find(
        `${el('CategoryListItem')} a[href$="${
          testProduct.subCategory
        }"]:visible`
      )
      .click();

    cy.location('href').should('include', testProduct.subCategory);

    cy.get(el('ProductListing', 'category', testProduct.subCategory)).as(
      'productList'
    );

    if (viewport !== 'mobile') {
      cy.get('@productList')
        .find(el('PanelHeader'))
        .contains(t('category', 'products.title'));
    }

    cy.get('@productList')
      .get(el('ProductCard'))
      .its('length')
      .should('be.gte', 2);

    // Sorting
    cy.get(el('SortOrderDropdown')).click();

    cy.get(
      `${el('SortOrderDropdown')} ${el('DropdownOption', 'value', 'price:asc')}`
    ).click();

    assertRouteLoad();

    const productPricesSelector = `${el(
      'ProductListing',
      'category',
      testProduct.subCategory
    )} ${el('ProductCard')} ${el('PriceLabel')}`;

    assertSorted({
      selector: productPricesSelector,
      parser: val => currency(val).value,
    });

    cy.get(el('SortOrderDropdown')).click();

    cy.get(
      `${el('SortOrderDropdown')} ${el(
        'DropdownOption',
        'value',
        'price:desc'
      )}`
    ).click();

    assertRouteLoad();

    assertSorted({
      selector: productPricesSelector,
      parser: val => currency(val).value,
      comparator: (a, b) => b - a,
    });

    // ... more test code
  }
);

// Some helper functions used in the above test code

export function assertRouteLoad() {
  cy.get(el('RouteLoader'), {
    timeout: Cypress.config('pageLoadTimeout'),
  }).should('exist');
  cy.get(el('RouteLoader'), {
    timeout: Cypress.config('pageLoadTimeout'),
  }).should('not.exist');
}

export function assertSorted({
  selector,
  timeout = Cypress.config('pageLoadTimeout'),
  parser = val => val,
  comparator = (a, b) => a - b,
}) {
  const startTime = Date.now();
  return new Cypress.Promise((resolve, reject) => {
    (function poll() {
      cy.get(selector).then(elements => {
        const values = elements
          .toArray()
          .map(element => parser(element.innerText));

        const sortedValues = values.slice().sort(comparator);
        const isSorted = equal(values, sortedValues);

        if (isSorted) {
          expect(values).to.eql(sortedValues);
          resolve(values);
          return;
        }

        if (startTime + timeout < Date.now()) {
          expect(values).to.eql(sortedValues);
          reject();
          return;
        }

        setTimeout(poll, 500);
      });
    })();
  });
}

// el(...) is a helper function that generates selector strings

This is the error:
failing test

As can be seen in the test code, cy.get() is never executed inside a cy.click() promise, which makes the error message kind of odd.

@zkilleb
Copy link

zkilleb commented Sep 5, 2019

capture8
Capture9

I am also experiencing this issue with cy.type() and cy.clear(). I only saw this issue when upgrading from 3.1.5 to 3.4.1. It happens at random in a specific test suite. It runs without issue headless but fails in Chrome. I installed 3.3.2 to see if that threw the same error message, and it does.

@DamienCassou
Copy link

Do you use the cypress-watch-and-reload plugin? Can you reproduce without it?

@prostko
Copy link

prostko commented Mar 31, 2023

Why did this issue get closed? There does not seem to be a resolution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg/driver This is due to an issue in the packages/driver directory
Projects
None yet
Development

No branches or pull requests