testingautomated-testscypressintegration-testinguncaught-exception

Cypress test fails on uncaught exception from my app - support/index.js is not working


This error is originationg from my app

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'links')

But in my support/index.js I wrote

import './commands'

Cypress.on('uncaught:exception', (err, runnable, promise) => {
    if (err.message.includes("Unexpected token ':'")) {
        return false
    }
    if (err.message.includes("TypeError")) {
        return false
    }
    if (err.message.includes("Uncaught")) {
        return false
    }
})

Why cant I catch the TypeError with if err.message.includes("TypeError")

Cypress version 9.1


Solution

  • "TypeError" is from the property err.name not err.message

    Cypress.on('uncaught:exception', (err, runnable, promise) => {
    
      // if (err.message.includes("TypeError")) {
      //     return false
      // }
    
      if (err.name === 'TypeError') {
          return false
      }
    })
    

    The console output for an error combines properties, so it's hard to tell what comes from where.

    To check all the properties of err, use spread operator

    console.log({...err})