@@ -123,7 +123,10 @@ export function renderWithFileRoutes(
123
123
}
124
124
125
125
// Helper to test specific file routes
126
- export function createMockFileRoute(path : string , component : React .ComponentType ) {
126
+ export function createMockFileRoute(
127
+ path : string ,
128
+ component : React .ComponentType ,
129
+ ) {
127
130
// This is useful for isolated testing when you don't want to use the full route tree
128
131
return {
129
132
path ,
@@ -165,7 +168,7 @@ describe('Generated Route Tree', () => {
165
168
}
166
169
167
170
const routePaths = getAllRoutePaths (routeTree )
168
-
171
+
169
172
// Test that expected routes are present
170
173
expect (routePaths ).toContain (' /' )
171
174
expect (routePaths ).toContain (' /about' )
@@ -174,9 +177,11 @@ describe('Generated Route Tree', () => {
174
177
175
178
it (' should have correct route hierarchy' , () => {
176
179
// Test parent-child relationships
177
- const homeRoute = routeTree .children ?.find ((child : any ) => child .path === ' /' )
180
+ const homeRoute = routeTree .children ?.find (
181
+ (child : any ) => child .path === ' /' ,
182
+ )
178
183
expect (homeRoute ).toBeDefined ()
179
-
184
+
180
185
// Test for specific route structure based on your file organization
181
186
// For example, if you have /posts/$postId routes:
182
187
// const postsRoute = routeTree.children?.find((child: any) => child.path === '/posts')
@@ -415,7 +420,7 @@ describe('Programmatic Navigation', () => {
415
420
await user .click (screen .getByRole (' button' , { name: / search/ i }))
416
421
417
422
expect (router .state .location .search ).toMatchObject ({
418
- q: ' test query'
423
+ q: ' test query' ,
419
424
})
420
425
})
421
426
})
@@ -447,9 +452,9 @@ describe('File-Based Route Guards', () => {
447
452
})
448
453
449
454
it (' should allow authenticated users to access protected routes' , () => {
450
- const mockAuth = {
451
- isAuthenticated: true ,
452
- user: { id: ' 1' , name: ' John' }
455
+ const mockAuth = {
456
+ isAuthenticated: true ,
457
+ user: { id: ' 1' , name: ' John' },
453
458
}
454
459
455
460
renderWithFileRoutes (<div />, {
@@ -475,12 +480,12 @@ describe('File-Based Route Loaders', () => {
475
480
const mockFetchPost = vi .fn ().mockResolvedValue ({
476
481
id: ' 123' ,
477
482
title: ' Test Post' ,
478
- content: ' Test content'
483
+ content: ' Test content' ,
479
484
})
480
485
481
486
// If your route loader uses a global API function, mock it
482
487
vi .mock (' ../api/posts' , () => ({
483
- fetchPost: mockFetchPost
488
+ fetchPost: mockFetchPost ,
484
489
}))
485
490
486
491
renderWithFileRoutes (<div />, {
@@ -498,7 +503,7 @@ describe('File-Based Route Loaders', () => {
498
503
const mockFetchPost = vi .fn ().mockRejectedValue (new Error (' Post not found' ))
499
504
500
505
vi .mock (' ../api/posts' , () => ({
501
- fetchPost: mockFetchPost
506
+ fetchPost: mockFetchPost ,
502
507
}))
503
508
504
509
renderWithFileRoutes (<div />, {
@@ -576,24 +581,26 @@ describe('File Route Error Handling', () => {
576
581
Route: {
577
582
component : () => {
578
583
throw new Error (' Test error' )
579
- }
580
- }
584
+ },
585
+ },
581
586
}))
582
587
583
588
renderWithFileRoutes (<div />, {
584
589
initialLocation: ' /error-prone' ,
585
590
})
586
591
587
592
expect (screen .getByText (/ Something went wrong/ )).toBeInTheDocument ()
588
-
593
+
589
594
consoleSpy .mockRestore ()
590
595
})
591
596
592
597
it (' should handle loader errors with error component' , async () => {
593
- const mockFailingLoader = vi .fn ().mockRejectedValue (new Error (' Load failed' ))
598
+ const mockFailingLoader = vi
599
+ .fn ()
600
+ .mockRejectedValue (new Error (' Load failed' ))
594
601
595
602
vi .mock (' ../api/data' , () => ({
596
- loadData: mockFailingLoader
603
+ loadData: mockFailingLoader ,
597
604
}))
598
605
599
606
renderWithFileRoutes (<div />, {
@@ -620,13 +627,13 @@ describe('Generated Route Types', () => {
620
627
it (' should provide type-safe navigation' , () => {
621
628
function TestComponent() {
622
629
const navigate = useNavigate ()
623
-
630
+
624
631
const handleNavigate = () => {
625
632
// This should be type-safe based on your generated routes
626
633
navigate ({
627
634
to: ' /posts/$postId' ,
628
635
params: { postId: ' 123' },
629
- search: { tab: ' comments' }
636
+ search: { tab: ' comments' },
630
637
})
631
638
}
632
639
@@ -665,10 +672,10 @@ describe('Route Tree Development', () => {
665
672
it (' should regenerate routes when files change' , () => {
666
673
// This test ensures your route tree is properly generated
667
674
// You can add specific assertions based on your file structure
668
-
675
+
669
676
expect (routeTree ).toBeDefined ()
670
677
expect (typeof routeTree .children ).toBe (' object' )
671
-
678
+
672
679
// Test specific routes exist
673
680
const routes = getAllRouteIds (routeTree )
674
681
expect (routes ).toContain (' /' )
@@ -703,7 +710,9 @@ Create `e2e/file-routing.spec.ts`:
703
710
import { test , expect } from ' @playwright/test'
704
711
705
712
test .describe (' File-Based Route E2E' , () => {
706
- test (' should navigate through file-based route structure' , async ({ page }) => {
713
+ test (' should navigate through file-based route structure' , async ({
714
+ page ,
715
+ }) => {
707
716
await page .goto (' /' )
708
717
709
718
// Test home route (from src/routes/index.tsx)
@@ -753,7 +762,7 @@ import { createFileRoute } from '@tanstack/react-router'
753
762
export const createMockFileRoute = (
754
763
path : string ,
755
764
component : React .ComponentType ,
756
- options : any = {}
765
+ options : any = {},
757
766
) => {
758
767
return createFileRoute (path )({
759
768
component ,
@@ -785,7 +794,7 @@ describe('Route Discovery', () => {
785
794
it (' should discover all routes from file structure' , () => {
786
795
// Test that your route tree includes all expected routes
787
796
// This helps catch when routes are accidentally not being generated
788
-
797
+
789
798
const expectedRoutes = [
790
799
' /' ,
791
800
' /about' ,
@@ -795,7 +804,7 @@ describe('Route Discovery', () => {
795
804
' /dashboard/settings' ,
796
805
]
797
806
798
- expectedRoutes .forEach (routePath => {
807
+ expectedRoutes .forEach (( routePath ) => {
799
808
const routeExists = checkRouteExists (routeTree , routePath )
800
809
expect (routeExists ).toBe (true )
801
810
})
@@ -844,7 +853,7 @@ describe('Posts Route (/posts)', () => {
844
853
renderWithFileRoutes (<div />, {
845
854
initialLocation: ' /posts' ,
846
855
})
847
-
856
+
848
857
expect (screen .getByText (/ Posts/ )).toBeInTheDocument ()
849
858
})
850
859
@@ -876,11 +885,13 @@ describe('Dashboard Routes', () => {
876
885
### Common Issues
877
886
878
887
** Problem** : Route tree not found in tests
888
+
879
889
``` bash
880
890
Error: Cannot find module ' ../routeTree.gen'
881
891
```
882
892
883
893
** Solution** : Ensure route tree generation in test setup:
894
+
884
895
``` ts
885
896
// vitest.config.ts
886
897
export default defineConfig ({
@@ -897,6 +908,7 @@ export default defineConfig({
897
908
** Problem** : Routes not updating in tests after file changes
898
909
899
910
** Solution** : Clear module cache in test setup:
911
+
900
912
``` ts
901
913
// src/test/setup.ts
902
914
beforeEach (() => {
@@ -909,6 +921,7 @@ beforeEach(() => {
909
921
** Problem** : Type errors in tests with generated routes
910
922
911
923
** Solution** : Ensure proper TypeScript configuration:
924
+
912
925
``` json
913
926
{
914
927
"compilerOptions" : {
@@ -934,4 +947,4 @@ After setting up file-based route testing, you might want to:
934
947
- [ TanStack Router File-Based Routing] ( ../routing/file-based-routing.md ) - Complete file-based routing guide
935
948
- [ File Naming Conventions] ( ../routing/file-naming-conventions.md ) - Understanding file structure
936
949
- [ Testing Library] ( https://testing-library.com/ ) - Component testing utilities
937
- - [ Vitest] ( https://vitest.dev/ ) - Testing framework documentation
950
+ - [ Vitest] ( https://vitest.dev/ ) - Testing framework documentation
0 commit comments