-
Notifications
You must be signed in to change notification settings - Fork 26k
/
app-types.test.js
109 lines (91 loc) · 3.89 KB
/
app-types.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* eslint-env jest */
import path from 'path'
import fs from 'fs-extra'
import { nextBuild } from 'next-test-utils'
const appDir = __dirname
describe('app type checking', () => {
;(process.env.TURBOPACK ? describe.skip : describe)('production mode', () => {
let stderr, errors
beforeAll(async () => {
stderr = (await nextBuild(appDir, [], { stderr: true })).stderr
errors = stderr.match(
/===== TS errors =====(.+)===== TS errors =====/s
)?.[1]
})
it('should generate route types correctly and report link error', async () => {
// Make sure the d.ts file is generated
const dts = (
await fs.readFile(path.join(appDir, '.next', 'types', 'link.d.ts'))
).toString()
expect(dts.includes('`/dashboard/user/')).toBeTruthy()
expect(dts.includes('`/dashboard/another')).toBeTruthy()
// Check type checking errors
expect(errors).toContain(
'Type error: "/(newroot)/dashboard/another" is not an existing route. If it is intentional, please type it explicitly with `as Route`.'
)
// Make sure all errors were reported and other links passed type checking
const errorLines = [
...errors.matchAll(
/\.\/src\/app\/type-checks\/link\/page\.tsx:(\d+):/g
),
].map(([, line]) => +line)
const ST = 17
const ED = 34
expect(errorLines).toEqual(
Array.from({ length: ED - ST + 1 }, (_, i) => i + ST)
)
})
it('should generate route types correctly and report router API errors', async () => {
// Make sure all errors were reported and other links passed type checking
const errorLines = [
...errors.matchAll(
/\.\/src\/app\/type-checks\/router\/page\.tsx:(\d+):/g
),
].map(([, line]) => +line)
const ST = 11
const ED = 13
expect(errorLines).toEqual(
Array.from({ length: ED - ST + 1 }, (_, i) => i + ST)
)
})
it('should type check invalid entry exports', () => {
// Can't export arbitrary things.
expect(errors).toContain(`"foo" is not a valid Page export field.`)
// Can't export invalid fields.
expect(errors).toMatch(
/Invalid configuration "revalidate":\s+Expected "false | number (>= 0)", got "-1"/
)
// Avoid invalid argument types for exported functions.
expect(errors).toMatch(
/Page "src\/app\/type-checks\/config\/page\.tsx" has an invalid "default" export:\s+Type "{ foo: string; }" is not valid/
)
expect(errors).toMatch(
/Page "src\/app\/type-checks\/config\/page\.tsx" has an invalid "generateMetadata" export:\s+Type "{ s: number; }" is not valid/
)
expect(errors).toMatch(
/Page "src\/app\/type-checks\/config\/page\.tsx" has an invalid "generateStaticParams" export:\s+Type "string" is not valid/
)
// Avoid invalid return types for exported functions.
expect(errors).toContain(
`"Promise<number>" is not a valid generateStaticParams return type`
)
// Can't export arbitrary things.
expect(errors).toContain(`"bar" is not a valid Route export field.`)
// Can't export invalid fields.
expect(errors).toMatch(
/Invalid configuration "revalidate":\s+Expected "false | number (>= 0)", got "-1"/
)
// Avoid invalid argument types for exported functions.
expect(errors).toMatch(
/Route "src\/app\/type-checks\/route-handlers\/route\.ts" has an invalid "GET" export:\s+Type "boolean" is not a valid type for the function's first argument/
)
expect(errors).toMatch(
/Route "src\/app\/type-checks\/route-handlers\/route\.ts" has an invalid "generateStaticParams" export:\s+Type "string" is not valid/
)
// Avoid invalid return types for exported functions.
expect(errors).toContain(
`"Promise<boolean>" is not a valid generateStaticParams return type`
)
})
})
})