-
Notifications
You must be signed in to change notification settings - Fork 27.5k
/
Copy pathwrite-app-declarations.test.ts
113 lines (99 loc) · 3.15 KB
/
write-app-declarations.test.ts
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
110
111
112
113
/* eslint-env jest */
import os from 'os'
import fs from 'fs-extra'
import { join } from 'path'
import { writeAppTypeDeclarations } from 'next/dist/lib/typescript/writeAppTypeDeclarations'
const fixtureDir = join(__dirname, 'fixtures/app-declarations')
const declarationFile = join(fixtureDir, 'next-env.d.ts')
const imageImportsEnabled = false
describe('find config', () => {
beforeEach(async () => {
await fs.ensureDir(fixtureDir)
})
afterEach(() => fs.remove(declarationFile))
it('should preserve CRLF EOL', async () => {
const eol = '\r\n'
const content =
'/// <reference types="next" />' +
eol +
(imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + eol
: '') +
eol +
'// NOTE: This file should not be edited' +
eol +
'// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.' +
eol
await fs.writeFile(declarationFile, content)
await writeAppTypeDeclarations({
baseDir: fixtureDir,
imageImportsEnabled,
hasPagesDir: false,
hasAppDir: false,
})
expect(await fs.readFile(declarationFile, 'utf8')).toBe(content)
})
it('should preserve LF EOL', async () => {
const eol = '\n'
const content =
'/// <reference types="next" />' +
eol +
(imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + eol
: '') +
eol +
'// NOTE: This file should not be edited' +
eol +
'// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.' +
eol
await fs.writeFile(declarationFile, content)
await writeAppTypeDeclarations({
baseDir: fixtureDir,
imageImportsEnabled,
hasPagesDir: false,
hasAppDir: false,
})
expect(await fs.readFile(declarationFile, 'utf8')).toBe(content)
})
it('should use OS EOL by default', async () => {
const eol = os.EOL
const content =
'/// <reference types="next" />' +
eol +
(imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + eol
: '') +
eol +
'// NOTE: This file should not be edited' +
eol +
'// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.' +
eol
await writeAppTypeDeclarations({
baseDir: fixtureDir,
imageImportsEnabled,
hasPagesDir: false,
hasAppDir: false,
})
expect(await fs.readFile(declarationFile, 'utf8')).toBe(content)
})
it('should include navigation types if app directory is enabled', async () => {
await writeAppTypeDeclarations({
baseDir: fixtureDir,
imageImportsEnabled,
hasPagesDir: false,
hasAppDir: true,
})
await expect(fs.readFile(declarationFile, 'utf8')).resolves.not.toContain(
'next/navigation-types/compat/navigation'
)
await writeAppTypeDeclarations({
baseDir: fixtureDir,
imageImportsEnabled,
hasPagesDir: true,
hasAppDir: true,
})
await expect(fs.readFile(declarationFile, 'utf8')).resolves.toContain(
'next/navigation-types/compat/navigation'
)
})
})