mirror of
https://github.com/cypress-io/cypress.git
synced 2026-05-24 17:38:52 -05:00
b52ac98a69
Co-authored-by: Renovate Bot <bot@renovateapp.com> Co-authored-by: Zach Bloomquist <git@chary.us>
51 lines
1.4 KiB
JavaScript
51 lines
1.4 KiB
JavaScript
/// <reference types="cypress" />
|
|
import { ErrorBoundary } from './error-boundary.jsx'
|
|
import React from 'react'
|
|
import { mount } from '@cypress/react'
|
|
|
|
Cypress.on('uncaught:exception', (err, runnable) => {
|
|
return false
|
|
})
|
|
|
|
/* eslint-env mocha */
|
|
describe('Error Boundary', () => {
|
|
const errorMessage = 'I crashed!'
|
|
const ChildWithoutError = () => <h1>Normal Child</h1>
|
|
const ChildWithError = () => {
|
|
throw new Error(errorMessage)
|
|
}
|
|
|
|
it('renders normal children', () => {
|
|
mount(
|
|
<ErrorBoundary>
|
|
<ChildWithoutError />
|
|
</ErrorBoundary>,
|
|
)
|
|
|
|
cy.get('h1').should('have.text', 'Normal Child')
|
|
cy.get(ErrorBoundary)
|
|
.its('state.error')
|
|
.should('not.exist')
|
|
})
|
|
|
|
it('on error, display fallback UI', () => {
|
|
// Error boundaries do not stop an uncaught error from propagating.
|
|
// Cypress will fail on uncaught exceptions by default, so we need to suppress that behavior.
|
|
cy.on('uncaught:exception', (err) => {
|
|
// Assert that we are only suppressing the default behavior for the error we expect.
|
|
expect(err.message.includes('I crashed!')).to.be.true
|
|
|
|
return false
|
|
})
|
|
|
|
mount(
|
|
<ErrorBoundary name="ChildWithError">
|
|
<ChildWithError />
|
|
</ErrorBoundary>,
|
|
)
|
|
|
|
cy.get('header h1').should('contain', 'Something went wrong.')
|
|
cy.get('header h3').should('contain', 'ChildWithError failed to load')
|
|
})
|
|
})
|