Skip to content

Commit ecfe14b

Browse files
docs: how-to guide for file-based router testing (#4776)
Add a new how-to guide for testing TanStack Router with file-based routing to cover specific patterns and challenges. --- [Open in Web](https://cursor.com/agents?id=bc-ee0f6db1-18b2-4c3b-b69c-13ea7f849190) • [Open in Cursor](https://cursor.com/background-agent?bcId=bc-ee0f6db1-18b2-4c3b-b69c-13ea7f849190) Learn more about [Background Agents](https://docs.cursor.com/background-agent/web-and-mobile) --------- Co-authored-by: Cursor Agent <[email protected]>
1 parent 1d34e56 commit ecfe14b

File tree

3 files changed

+975
-22
lines changed

3 files changed

+975
-22
lines changed

docs/router/framework/react/how-to/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ This directory contains focused, step-by-step instructions for common TanStack R
2222

2323
### Testing & Debugging
2424

25-
- [Set Up Testing](./setup-testing.md) - Comprehensive testing setup with Vitest, React Testing Library, and Playwright
25+
- [Set Up Testing with Code-Based Routing](./setup-testing.md) - Comprehensive testing setup for manually defined routes
26+
- [Test File-Based Routing](./test-file-based-routing.md) - Specific patterns for testing file-based routing applications
2627
- [Debug Router Issues](./debug-router-issues.md) - Troubleshoot common routing problems and performance issues
2728

2829
### UI Library Integration

docs/router/framework/react/how-to/setup-testing.md

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# How to Set Up Testing with TanStack Router
1+
# How to Set Up Testing with Code-Based Routing
22

3-
This guide covers setting up comprehensive testing for TanStack Router applications, including unit tests, integration tests, and end-to-end testing strategies.
3+
This guide covers setting up comprehensive testing for TanStack Router applications that use code-based routing, including unit tests, integration tests, and end-to-end testing strategies.
44

55
## Quick Start
66

7-
Set up testing by configuring your test framework (Vitest/Jest), creating router test utilities, and implementing patterns for testing navigation, route components, and data loading.
7+
Set up testing by configuring your test framework (Vitest/Jest), creating router test utilities, and implementing patterns for testing navigation, route components, and data loading with manually defined routes.
8+
9+
> **Using File-Based Routing?** See [How to Test File-Based Routing](./test-file-based-routing.md) for patterns specific to file-based routing applications.
810
911
---
1012

@@ -56,7 +58,9 @@ global.IS_REACT_ACT_ENVIRONMENT = true
5658

5759
---
5860

59-
## Router Testing Patterns
61+
## Code-Based Router Testing Patterns
62+
63+
The following patterns are specifically designed for applications using code-based routing where you manually create routes with `createRoute()` and build route trees programmatically.
6064

6165
### 1. TanStack Router Internal Pattern (Recommended)
6266

@@ -227,7 +231,7 @@ export function ErrorComponent({ error }: { error: Error }) {
227231

228232
---
229233

230-
## Test Router Components
234+
## Test Code-Based Route Components
231235

232236
### 1. Basic Component Testing
233237

@@ -241,7 +245,7 @@ import {
241245
TestComponent,
242246
} from '../test/router-utils'
243247

244-
describe('Router Component Testing', () => {
248+
describe('Code-Based Route Component Testing', () => {
245249
it('should render route component', () => {
246250
const testRoute = createRoute({
247251
getParentRoute: () => rootRoute,
@@ -356,7 +360,7 @@ import {
356360
TestComponent,
357361
} from '../test/router-utils'
358362

359-
describe('Navigation Testing', () => {
363+
describe('Code-Based Route Navigation', () => {
360364
it('should navigate when link is clicked', async () => {
361365
const user = userEvent.setup()
362366

@@ -458,7 +462,7 @@ import { screen } from '@testing-library/react'
458462
import { createRoute, redirect } from '@tanstack/react-router'
459463
import { renderWithRouter, rootRoute } from '../test/router-utils'
460464

461-
describe('Route Guards', () => {
465+
describe('Code-Based Route Guards', () => {
462466
it('should redirect unauthenticated users', () => {
463467
const mockAuth = { isAuthenticated: false }
464468

@@ -536,7 +540,7 @@ import { screen, waitFor } from '@testing-library/react'
536540
import { createRoute } from '@tanstack/react-router'
537541
import { renderWithRouter, rootRoute } from '../test/router-utils'
538542

539-
describe('Data Loading', () => {
543+
describe('Code-Based Route Data Loading', () => {
540544
it('should load and display data from loader', async () => {
541545
const mockFetchUser = vi.fn().mockResolvedValue({
542546
id: 1,
@@ -697,7 +701,7 @@ interface RouterContext {
697701
}
698702
}
699703

700-
describe('Router Context', () => {
704+
describe('Code-Based Router Context', () => {
701705
it('should provide context to routes', () => {
702706
const rootRouteWithContext = createRootRouteWithContext<RouterContext>()({
703707
component: () => <Outlet />,
@@ -783,7 +787,7 @@ Create `e2e/navigation.spec.ts`:
783787
```ts
784788
import { test, expect } from '@playwright/test'
785789

786-
test.describe('Router Navigation', () => {
790+
test.describe('Code-Based Router Navigation', () => {
787791
test('should navigate between pages', async ({ page }) => {
788792
await page.goto('/')
789793

@@ -837,7 +841,7 @@ test.describe('Router Navigation', () => {
837841

838842
---
839843

840-
## Testing Best Practices
844+
## Code-Based Routing Testing Best Practices
841845

842846
### 1. Test Organization
843847

@@ -847,13 +851,13 @@ src/
847851
│ ├── Header.tsx
848852
│ └── Header.test.tsx
849853
├── routes/
850-
│ ├── posts/
851-
├── index.tsx
852-
└── index.test.tsx
854+
│ ├── posts.tsx # Code-based route definitions
855+
│ ├── posts.test.tsx
856+
│ └── index.tsx
853857
├── test/
854858
│ ├── setup.ts
855-
│ ├── router-utils.tsx
856-
│ └── mock-routes.tsx
859+
│ ├── router-utils.tsx # Code-based router utilities
860+
│ └── mock-routes.tsx # Manual route factories
857861
└── __tests__/
858862
├── integration/
859863
└── e2e/
@@ -862,15 +866,24 @@ src/
862866
### 2. Common Patterns
863867

864868
```tsx
865-
// Mock external dependencies
869+
// Mock external dependencies for code-based routes
866870
vi.mock('../api/users', () => ({
867871
fetchUser: vi.fn(),
868872
updateUser: vi.fn(),
869873
}))
870874

871-
// Test utility for common setups
875+
// Test utility for common code-based route setups
872876
export function createAuthenticatedRouter(user = mockUser) {
873-
return createTestRouter(routes, {
877+
// Manually create routes for testing
878+
const protectedRoutes = [
879+
createRoute({
880+
getParentRoute: () => rootRoute,
881+
path: '/dashboard',
882+
component: DashboardComponent,
883+
}),
884+
]
885+
886+
return createTestRouter(protectedRoutes, {
874887
context: {
875888
auth: { user, isAuthenticated: true },
876889
},
@@ -942,8 +955,9 @@ await waitFor(() => {
942955

943956
## Common Next Steps
944957

945-
After setting up testing, you might want to:
958+
After setting up code-based routing testing, you might want to:
946959

960+
- [How to Test File-Based Routing](./test-file-based-routing.md) - Specific patterns for file-based routing apps
947961
- [How to Set Up Basic Authentication](./setup-authentication.md) - Test authentication flows
948962
- [How to Debug Common Router Issues](./debug-router-issues.md) - Debug test failures
949963

@@ -954,6 +968,7 @@ After setting up testing, you might want to:
954968

955969
## Related Resources
956970

971+
- [Code-Based Routing Guide](../routing/code-based-routing.md) - Understanding code-based routing
957972
- [Vitest Documentation](https://vitest.dev/) - Testing framework
958973
- [Testing Library React](https://testing-library.com/docs/react-testing-library/intro/) - Component testing utilities
959974
- [Playwright Documentation](https://playwright.dev/) - E2E testing framework

0 commit comments

Comments
 (0)