Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import * as React from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vite-plus/test'
import { createReactI18nextMock } from '@/test/i18n-mock'
import ErrorBoundary, { withErrorBoundary } from '../index'
const mockConfig = vi.hoisted(() => ({
isDev: false,
}))
vi.mock('@/config', () => ({
get IS_DEV() {
return mockConfig.isDev
},
}))
vi.mock('react-i18next', () =>
createReactI18nextMock({
error: 'Error',
'errorBoundary.componentStack': 'Component Stack:',
'errorBoundary.details': 'Error Details (Development Only)',
'errorBoundary.errorCount': 'This error has occurred {{count}} times',
'errorBoundary.fallbackTitle': 'Oops! Something went wrong',
'errorBoundary.message': 'An unexpected error occurred while rendering this component.',
'errorBoundary.reloadPage': 'Reload Page',
'errorBoundary.title': 'Something went wrong',
'errorBoundary.tryAgain': 'Try Again',
'errorBoundary.tryAgainCompact': 'Try again',
}),
)
type ThrowOnRenderProps = {
message?: string
shouldThrow: boolean
}
const ThrowOnRender = ({ shouldThrow, message = 'render boom' }: ThrowOnRenderProps) => {
if (shouldThrow) throw new Error(message)
return <div>Child content rendered</div>
}
let consoleErrorSpy: ReturnType<typeof vi.spyOn>
describe('ErrorBoundary', () => {
beforeEach(() => {
vi.clearAllMocks()
mockConfig.isDev = false
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined)
})
afterEach(() => {
consoleErrorSpy.mockRestore()
vi.unstubAllGlobals()
})
describe('Rendering', () => {
it('should render children when no error occurs', () => {
render(
<ErrorBoundary>
<ThrowOnRender shouldThrow={false} />
</ErrorBoundary>,
)
expect(screen.getByText('Child content rendered')).toBeInTheDocument()
})
it('should render default fallback with title and message when child throws', async () => {
render(
<ErrorBoundary>
<ThrowOnRender shouldThrow={true} />
</ErrorBoundary>,
)
expect(await screen.findByText('Something went wrong')).toBeInTheDocument()
expect(
screen.getByText('An unexpected error occurred while rendering this component.'),
).toBeInTheDocument()
})
it('should render custom title and message in fallback', async () => {
render(
<ErrorBoundary
customMessage="Custom recovery message"
customTitle="Custom crash title"
isolate={false}
>
<ThrowOnRender shouldThrow={true} />
</ErrorBoundary>,
)
expect(await screen.findByText('Custom crash title')).toBeInTheDocument()
expect(screen.getByText('Custom recovery message')).toBeInTheDocument()
})
})
describe('Fallback props', () => {
it('should render node fallback when fallback prop is a React node', async () => {
render(
<ErrorBoundary fallback={<div>Node fallback content</div>}>
<ThrowOnRender shouldThrow={true} />
</ErrorBoundary>,
)
expect(await screen.findByText('Node fallback content')).toBeInTheDocument()
})
it('should render function fallback with error message when fallback prop is a function', async () => {
render(
<ErrorBoundary fallback={(error) => <div>Function fallback: {error.message}</div>}>
<ThrowOnRender message="function fallback boom" shouldThrow={true} />
</ErrorBoundary>,
)
expect(
await screen.findByText('Function fallback: function fallback boom'),
).toBeInTheDocument()
})
})
describe('Error reporting', () => {
it('should call onError with error and errorInfo when child throws', async () => {
const onError = vi.fn()
render(
<ErrorBoundary onError={onError}>
<ThrowOnRender shouldThrow={true} />
</ErrorBoundary>,
)
await screen.findByText('Something went wrong')
expect(onError).toHaveBeenCalledTimes(1)
expect(onError).toHaveBeenCalledWith(
expect.objectContaining({ message: 'render boom' }),
expect.objectContaining({ componentStack: expect.any(String) }),
)
})
it('should render details block when showDetails is true', async () => {
render(
<ErrorBoundary showDetails={true}>
<ThrowOnRender message="details boom" shouldThrow={true} />
</ErrorBoundary>,
)
expect(await screen.findByText('Error Details (Development Only)')).toBeInTheDocument()
expect(screen.getByText('Error:')).toBeInTheDocument()
expect(screen.getByText(/details boom/i)).toBeInTheDocument()
})
})
describe('Recovery', () => {
it('should hide recovery actions when enableRecovery is false', async () => {
render(
<ErrorBoundary enableRecovery={false}>
<ThrowOnRender shouldThrow={true} />
</ErrorBoundary>,
)
await screen.findByText('Something went wrong')
expect(screen.queryByRole('button', { name: 'Try Again' })).not.toBeInTheDocument()
expect(screen.queryByRole('button', { name: 'Reload Page' })).not.toBeInTheDocument()
})
it('should reset and render children when Try Again is clicked', async () => {
const user = userEvent.setup()
const onReset = vi.fn()
const RecoveryHarness = () => {
const [shouldThrow, setShouldThrow] = React.useState(true)
return (
<ErrorBoundary
onReset={() => {
onReset()
setShouldThrow(false)
}}
>
<ThrowOnRender shouldThrow={shouldThrow} />
</ErrorBoundary>
)
}
render(<RecoveryHarness />)
await user.click(await screen.findByRole('button', { name: 'Try Again' }))
await screen.findByText('Child content rendered')
expect(onReset).toHaveBeenCalledTimes(1)
})
it('should reset after resetKeys change when boundary is in error state', async () => {
const user = userEvent.setup()
const ResetKeysHarness = () => {
const [shouldThrow, setShouldThrow] = React.useState(true)
const [boundaryKey, setBoundaryKey] = React.useState(0)
return (
<>
<button
onClick={() => {
setShouldThrow(false)
setBoundaryKey(1)
}}
>
Recover with keys
</button>
<ErrorBoundary resetKeys={[boundaryKey]}>
<ThrowOnRender shouldThrow={shouldThrow} />
</ErrorBoundary>
</>
)
}
render(<ResetKeysHarness />)
await screen.findByText('Something went wrong')
await user.click(screen.getByRole('button', { name: 'Recover with keys' }))
await waitFor(() => {
expect(screen.getByText('Child content rendered')).toBeInTheDocument()
})
})
it('should not reset when resetKeys reference changes but values are identical', async () => {
const user = userEvent.setup()
const onReset = vi.fn()
const StableKeysHarness = () => {
const [keys, setKeys] = React.useState<Array<string | number>>([1, 2])
return (
<>
<button onClick={() => setKeys([1, 2])}>Update keys same values</button>
<ErrorBoundary resetKeys={keys} onReset={onReset}>
<ThrowOnRender shouldThrow={true} />
</ErrorBoundary>
</>
)
}
render(<StableKeysHarness />)
await screen.findByText('Something went wrong')
await user.click(screen.getByRole('button', { name: 'Update keys same values' }))
await waitFor(() => {
expect(screen.getByText('Something went wrong')).toBeInTheDocument()
})
expect(onReset).not.toHaveBeenCalled()
})
it('should reset after children change when resetOnPropsChange is true', async () => {
const user = userEvent.setup()
const ResetOnPropsHarness = () => {
const [shouldThrow, setShouldThrow] = React.useState(true)
const [childLabel, setChildLabel] = React.useState('first child')
return (
<>
<button
onClick={() => {
setShouldThrow(false)
setChildLabel('second child')
}}
>
Replace children
</button>
<ErrorBoundary resetOnPropsChange={true}>
{shouldThrow ? <ThrowOnRender shouldThrow={true} /> : <div>{childLabel}</div>}
</ErrorBoundary>
</>
)
}
render(<ResetOnPropsHarness />)
await screen.findByText('Something went wrong')
await user.click(screen.getByRole('button', { name: 'Replace children' }))
await waitFor(() => {
expect(screen.getByText('second child')).toBeInTheDocument()
})
})
it('should call window.location.reload when Reload Page is clicked', async () => {
const user = userEvent.setup()
const reloadSpy = vi.fn()
vi.stubGlobal('location', { ...window.location, reload: reloadSpy })
render(
<ErrorBoundary>
<ThrowOnRender shouldThrow={true} />
</ErrorBoundary>,
)
await user.click(await screen.findByRole('button', { name: 'Reload Page' }))
expect(reloadSpy).toHaveBeenCalledTimes(1)
})
})
it.each([{ resetKeys: [] }, { resetKeys: undefined }, { resetKeys: ['first'] }])(
'recovers when reset keys are removed: $resetKeys',
({ resetKeys }) => {
const onReset = vi.fn()
const { rerender } = render(
<ErrorBoundary resetKeys={['first', 'second']} onReset={onReset}>
<ThrowOnRender shouldThrow />
</ErrorBoundary>,
)
expect(screen.getByText('Something went wrong')).toBeInTheDocument()
rerender(
<ErrorBoundary resetKeys={resetKeys} onReset={onReset}>
<ThrowOnRender shouldThrow={false} />
</ErrorBoundary>,
)
expect(screen.getByText('Child content rendered')).toBeInTheDocument()
expect(onReset).toHaveBeenCalledOnce()
},
)
})
describe('ErrorBoundary utility exports', () => {
beforeEach(() => {
vi.clearAllMocks()
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined)
})
afterEach(() => {
consoleErrorSpy.mockRestore()
vi.unstubAllGlobals()
})
describe('withErrorBoundary', () => {
it('should wrap component and render custom title when wrapped component throws', async () => {
type WrappedProps = {
shouldThrow: boolean
}
const WrappedTarget = ({ shouldThrow }: WrappedProps) => {
if (shouldThrow) throw new Error('wrapped boom')
return <div>Wrapped content</div>
}
const Wrapped = withErrorBoundary(WrappedTarget, {
customTitle: 'Wrapped boundary title',
})
render(<Wrapped shouldThrow={true} />)
expect(await screen.findByText('Wrapped boundary title')).toBeInTheDocument()
})
})
})