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.

wrap().invoke().should() ignores timeout options in should assertion

See original GitHub issue

When a function wrapped inside a wrap gets invoked by a invoke, the should seems to ignore the timeout passed along in the wrap object.

Test code:

const slow = () => new Cypress.Promise(resolve => {
  setTimeout(() => resolve('done'), 2000);
});

cy
  .wrap({ slow }, { timeout: 100 })
  .invoke('slow')
  .should('eq', 'done');

This test is a silly example, in reality I’m querying the Mandrill API, which I want to give a very big timeout as Mandrill can take quite a while to show an email, but I do not want to update the defaultCommandTimeout to minutes as other assertions will never take this long.

Additional Info (images, stack traces, etc)

screen shot 2018-01-24 at 11 41 35

screen shot 2018-01-24 at 11 41 51

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
peterklijncommented, Sep 17, 2018

@jennifer-shehane is there an update on this issue? It is still present in version 3.1.0.

@mitchkm you can do something like this, it’s horrible but it works:

let defaultTimeout;

before(() => {
  // Store default timeout
  defaultTimeout = Cypress.config('defaultCommandTimeout');
});

beforeEach(() => {
  // Set the timeout to something high
  Cypress.config('defaultCommandTimeout', 5 * 60 * 1000); // 5 minutes
});

it('long test', () => {
  const slow = () => new Cypress.Promise(resolve => {
    setTimeout(() => resolve('done'), 2 * 60 * 1000);
  });
  
  cy
    .wrap({ slow })
    .invoke('slow')
    .should('eq', 'done')
    // Change the timeout back to the default so it doesn't effect other tests.
    .then(() => Cypress.config('defaultCommandTimeout', defaultTimeout));
});
0reactions
donleqtcommented, Mar 19, 2020

Here is the solution and example:


/**
 *  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;
  });
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

should | Cypress Documentation
Create an assertion. Assertions are automatically retried until they pass or time out. An alias of .and() Note: .should() assumes you are already....
Read more >
Assertions | Cypress examples (v9.7.0) - Gleb Bahmutov
To make an assertion about the current subject, use the .should() command. ... expression // first need to invoke jQuery method text() //...
Read more >
Timeout on a function call - python - Stack Overflow
Process(target=bar) p.start() # Wait for 10 seconds or until process finishes ... if process is stuck for good p.terminate() # OR Kill -...
Read more >
gMock Cookbook | GoogleTest
Perhaps you want to do it as part of a stub action, or perhaps your test doesn't need to mock Concrete() at all...
Read more >
Mocha - the fun, simple, flexible JavaScript test framework
chai - expect() , assert() and should -style assertions; better-assert - C-style self-documenting assert(); unexpected - “the extensible BDD assertion toolkit” ...
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