Skip to content

Commit 7ef0a2b

Browse files
authored
test: improve tests for typed route validation (#82660)
Remove tests of the implementation details. Add more tests for route handler types. Re: feedback from @eps1lon
1 parent e90efec commit 7ef0a2b

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

test/e2e/app-dir/typed-routes-validator/typed-routes-validator.test.ts

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,8 @@ describe('typed-routes-validator', () => {
1212

1313
it('should generate route validation correctly', async () => {
1414
const dts = await next.readFile('.next/types/validator.ts')
15-
expect(dts).toMatch(
16-
/const handler = {} as typeof import\(".*\/app\/page.js"\)\s+handler satisfies AppPageConfig<"\/">/
17-
)
18-
expect(dts).toMatch(
19-
/const handler = {} as typeof import\(".*\/app\/send-email\/route.js"\)\s+handler satisfies RouteHandlerConfig<"\/send-email">/
20-
)
21-
expect(dts).toMatch(
22-
/const handler = {} as typeof import\(".*\/pages\/about.js"\)\s+handler satisfies PagesPageConfig/
23-
)
24-
expect(dts).toMatch(
25-
/const handler = {} as typeof import\(".*\/pages\/api\/test-route.js"\)\s+handler satisfies ApiRouteConfig/
26-
)
27-
expect(dts).toMatch(
28-
/const handler = {} as typeof import\(".*\/app\/layout.js"\)\s+handler satisfies LayoutConfig<"\/">/
29-
)
15+
// sanity check that dev generation is working
16+
expect(dts).toContain('handler satisfies AppPageConfig')
3017
})
3118

3219
if (isNextStart) {
@@ -81,6 +68,23 @@ describe('typed-routes-validator', () => {
8168
return new Response('Created', { status: 201 })
8269
}
8370
71+
export const dynamic = 'force-dynamic'
72+
`
73+
)
74+
75+
await next.patchFile(
76+
'app/valid-2/route.ts',
77+
`
78+
import type { NextRequest } from 'next/server'
79+
80+
export async function GET() {
81+
return new Response('OK')
82+
}
83+
84+
export async function POST(request: NextRequest) {
85+
return new Response('Created', { status: 201 })
86+
}
87+
8488
export const dynamic = 'force-dynamic'
8589
`
8690
)
@@ -89,7 +93,7 @@ describe('typed-routes-validator', () => {
8993
expect(exitCode).toBe(0)
9094
})
9195

92-
it('should fail type checking with invalid route handler exports', async () => {
96+
it('should fail type checking with invalid route handler return type', async () => {
9397
await next.stop()
9498
await next.patchFile(
9599
'app/invalid/route.ts',
@@ -111,6 +115,26 @@ describe('typed-routes-validator', () => {
111115
)
112116
})
113117

118+
it('should fail type checking with invalid route handler params', async () => {
119+
await next.stop()
120+
await next.patchFile(
121+
'app/invalid-2/route.ts',
122+
`
123+
// not a valid type for request
124+
export async function POST(request: number) {
125+
return new Response('Created', { status: 201 })
126+
}
127+
`
128+
)
129+
130+
const { exitCode, cliOutput } = await next.build()
131+
// clean up before assertion just in case it fails
132+
await next.deleteFile('app/invalid-2/route.ts')
133+
134+
expect(exitCode).toBe(1)
135+
expect(cliOutput).toContain(`Types of property 'POST' are incompatible.`)
136+
})
137+
114138
it('should pass type checking with valid layout exports', async () => {
115139
await next.stop()
116140
await next.patchFile(

0 commit comments

Comments
 (0)