1

I would like to log additional data when a test case fails. Where can I best insert my custom error handler?

For example:

cy.get('something')
  .should('have.property', 'blah')

When this fails in the Cypress dashboard, I get:

CypressError: Timed out retrying: expected 'something' to have a property 'blah'
  at Object.cypressErr (https://my.website.com/__cypress/runner/cypress_runner.js:65727:11)
  at Object.throwErr (https://my.website.com/__cypress/runner/cypress_runner.js:65692:18)
  at Object.throwErrByPath (https://my.website.com/__cypress/runner/cypress_runner.js:65719:17)
  at retry (https://my.website.com/__cypress/runner/cypress_runner.js:59237:16)
  at https://my.website.com/__cypress/runner/cypress_runner.js:51312:18
  at tryCatcher (https://my.website.com/__cypress/runner/cypress_runner.js:131273:23)
  at Promise._settlePromiseFromHandler (https://my.website.com/__cypress/runner/cypress_runner.js:129291:31)
  at Promise._settlePromise (https://my.website.com/__cypress/runner/cypress_runner.js:129348:18)
  at Promise._settlePromise0 (https://my.website.com/__cypress/runner/cypress_runner.js:129393:10)
  at Promise._settlePromises (https://my.website.com/__cypress/runner/cypress_runner.js:129468:18)
  at Async._drainQueue (https://my.website.com/__cypress/runner/cypress_runner.js:126197:16)
  at Async._drainQueues (https://my.website.com/__cypress/runner/cypress_runner.js:126207:10)
  at Async.drainQueues (https://my.website.com/__cypress/runner/cypress_runner.js:126081:14)
  at <anonymous>

What I can do to customize this error, to something like:

CypressError: ...

My additional logging: {foo: 'bar', etc, etc}

Ultimately, I want to log some additional contextual data, so I can investigate + debug why a test may be failing/flaking.

dot_zero
  • 1,030
  • 3
  • 12
  • 26

1 Answers1

0

I hope this library helps your need - https://github.com/cypress-io/error-message

Here is a gist out of it,

//Load the function from the module
const {formErrorText} = require('@cypress/error-message')

//Handle the error with custome messages
const fileSaveError = {
  description: 'We could not save an important file',
  solution: `Please check folder permissions and try again

    more details on our FAQ page: https://faq.company.name
  `
}
fs.writeFile(name)
  .catch(
    formErrorText(info).then(console.error)
  )
/*
  shows nice error message

  ------
  We could not save an important file
  Please check folder permissions and try again

    more details on our FAQ page: https://faq.company.name

  Exception message
  ------
  Platform: darwin
  Version: 15.6.2
*/

For more information, you can check this blog - https://www.cypress.io/blog/2017/07/26/good-error-messages/#Useful-E2E-assertion-failures

Edit 1:

As stated in comment, if you are looking for handling the mysterious failures, would suggest you to take a look at Cypress event handling mechanism. Here is the excerpt of it,

// likely want to do this in a support file
// so it's applied to all spec files
// cypress/support/index.js

Cypress.on('uncaught:exception', (err, runnable) => {
  // Handle your logging logic here
})
Kondasamy Jayaraman
  • 1,802
  • 1
  • 20
  • 25
  • Thank you! I'll give this a shot and report back if it works! – dot_zero Apr 23 '19 at 16:48
  • How would you attach this to Cypress? I don't think it offers `catch` in its Chainers. – dot_zero Apr 23 '19 at 16:51
  • This plugin offers error handling; would request you to follow the document mentioned in the solution. – Kondasamy Jayaraman Apr 24 '19 at 03:02
  • 1
    Unless I misunderstand, the library looks like it adds custom error messaging to regular errors. How does this work in a Cypress test? The example is for a standard `try/catch`. My goal is to add additional error context in a Cypress failure. Currently I can't get this info in the Dashboard, and I'm hoping to print more info on a mysterious test failure. – dot_zero Apr 24 '19 at 08:02
  • 1
    I have edited my answer to handle *uncaught exceptions*. Please check if this helps. – Kondasamy Jayaraman Apr 24 '19 at 09:54