cypress-wait-until: Catch 'errorMsg' if exceeds 'timeout'?

Viewed 219

Using npm module cypress-wait-until, is there a way to catch errorMsg if cy.waitUntil() has completed all possible intervals and exceeded timeout? Thereby, providing a point of failure.

Example code waiting for an entry to be found in a log. Custom command cy.cmdAvailable() response is Chainable<boolean>, returns true when a specific service is yet available, else false:

cy.waitUntil(() => cy.cmdAvailable()), {
  errorMsg: 'ERROR: Not Available',
  verbose: true,
  customCheckMessage: 'CHECK: Availability'
}).then($status => {
  expect($status).to.be.a('boolean');
});
1 Answers

cy.waitUntil() always throws an error on timeout, so options are limited.

You could precede the command with a listener on the fail event - Recipe Error handling

If you want the status inside .then(), use a flag variable,

let failed = false;

cy.once('fail', (event, runnable) => {
  if (e.message.includes('ERROR: Not Available')) {
    failed = true;
    return false  // not fail on this message
  })
})

cy.waitUntil(() => cy.cmdAvailable()), {
  errorMsg: 'ERROR: Not Available',
  verbose: true,
  customCheckMessage: 'CHECK: Availability'
}).then(() => {
  // use failed here
})

It's possible .then() does not get actioned because previous chained command failed.

If so, separate it from cy.waitUntil()

cy.waitUntil(() => cy.cmdAvailable()), {
  errorMsg: 'ERROR: Not Available',
  verbose: true,
  customCheckMessage: 'CHECK: Availability'
})

cy.then(() => {
  // use failed here
})
Related