Open History to see the last commit
1import { afterEach, describe, expect, test, vi } from 'vitest'
2import { cleanup, render, screen } from '@solidjs/testing-library'
3import { ErrorBoundary } from 'solid-js'
4import {
5 RouterProvider,
6 createRootRoute,
7 createRoute,
8 createRouter,
9} from '../src'
10import type { JSX } from 'solid-js'
11β
12function ThrowingComponent() {
13 throw new Error('Test error')
14}
15β
16// Custom error boundary to catch errors that bubble up
17function TestErrorBoundary(props: { children: JSX.Element }) {
18 return (
19 <ErrorBoundary
20 fallback={(err) => (
21 <div>External Error Boundary Caught: {err.message}</div>
22 )}
23 >
24 {props.children}
25 </ErrorBoundary>
26 )
27}
28β
29function createTestRouter(disableGlobalCatchBoundary: boolean) {
30 const rootRoute = createRootRoute({})
31 const indexRoute = createRoute({
32 getParentRoute: () => rootRoute,
33 path: '/',
34 component: ThrowingComponent,
35 })
36β
37 const routeTree = rootRoute.addChildren([indexRoute])
38 return createRouter({
39 routeTree,
40 disableGlobalCatchBoundary,
41 })
42}
43β
44afterEach(() => {
45 vi.resetAllMocks()
46 window.history.replaceState(null, 'root', '/')
47 cleanup()
48})
49β
50describe('disableGlobalCatchBoundary option', () => {
51 test('catches errors in global boundary when disableGlobalCatchBoundary is false', async () => {
52 const router = createTestRouter(false)
53β
54 render(() => <RouterProvider router={router} />)
55β
56 // The global CatchBoundary shows "Something went wrong!" by default
57 const errorElement = await screen.findByText('Something went wrong!')
58 expect(errorElement).toBeInTheDocument()
59 })
60β
61 test('errors bubble up to external error boundary when disableGlobalCatchBoundary is true', async () => {
62 const router = createTestRouter(true)
63β
64 // Wrap RouterProvider in an external error boundary
65 render(() => (
66 <TestErrorBoundary>
67 <RouterProvider router={router} />
68 </TestErrorBoundary>
69 ))
70β
71 // Error should bubble up and be caught by the external error boundary
72 const externalErrorElement = await screen.findByText(
73 'External Error Boundary Caught: Test error',
74 )
75 expect(externalErrorElement).toBeInTheDocument()
76 })
77})