question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cy.wrap does not respect timeout

See original GitHub issue

This example spec file fails with the error below:

describe('Cypress wait', () => {
  it('wrap does not wait', () => {
    cy.wrap(
        new Promise((resolve) => setTimeout(resolve, 5000)), {timeout: 20000});
  });
});
 Cypress wait
    1) wrap does not wait


  0 passing (4s)
  1 failing

  1) Cypress wait wrap does not wait:
     Error: Cypress command timeout of '4000ms' exceeded.
      at http://localhost:57838/__cypress/runner/cypress_runner.js:103532:25

Shouldn’t cy.wrap respect the timeout option? The test passes if I pass --config defaultCommandTimeout=6000 on the command line, but I want to pass a specific timeout for this cy.wrap command.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
donleqtcommented, Mar 19, 2020

Hello here is a workaround to force Cypress to wait for a promise:


/**
 *  helper function that forces cy to wait for a promise
 *
 * @export
 * @param {Promise} promise promise to wait
 * @param {number} [interval=1000] recheck in minlisecons
 * @returns
 */
export function waitPromise(promise, interval = 1000) {
  let isDone = false;

  const runPromise = () => {
    if (isDone) {
      // Wrap and returns the result
      return cy.wrap(promise.catch(error => assert.isNotOk(true, error)));
    }
    return cy.wait(interval).then(() => runPromise());
  };

  // Marks as resolved
  promise.then(() => (isDone = true)).catch(() => (isDone = true));

  return runPromise();
}

And in-use example

it('Restore Password - Should received an email', () => {
  const task = emailHelper.searchLastEmail({
    emailAddress: user.email,
    subject: 'Reset Password',
    timeout: 2 * 60000 // 2 min
  });

  testHelpers.waitPromise(task).then(mail => {
    // Save for later test
    restorePasswordMail = mail;
    return expect(mail).not.to.be.null;
  });
});
0reactions
cypress-bot[bot]commented, Jun 8, 2020

Released in 4.8.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to Cypress v4.8.0, please open a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress Test timeout on then() not respecting timeout option
I've been trying to figure out when the . then() function in Cypress is not respecting the timeout option. The then() function is...
Read more >
wrap - Cypress Documentation
If the promise is rejected, cy.wrap() will fail the test. cy.wrap() will automatically retry until all chained assertions have passed. Timeouts.
Read more >
cypress-io/cypress - Gitter
hey guys, Cypress does not seem to respect the pageLoadTimeout setting on the ... but then cy.wrap() times out and says CypressError: Timed...
Read more >
cypress-wait-until - npm
nested cy.waitUntil calls don't respect timeout options. timeout and interval are converted to a number of retries. If the parent ...
Read more >
8 Tricks I Learned From Cypress - Blog - ServMask
In other words, Cypress waits for a selector within a certain timeout. It's most likely that you're going to interact with DOM, so...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found