forked from vercel/next.js
/
index.test.js
128 lines (114 loc) · 3.54 KB
/
index.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import cheerio from 'cheerio'
import {
renderViaHTTP,
findPort,
launchApp,
nextBuild,
killApp,
check,
} from 'next-test-utils'
const appDir = join(__dirname, '..')
let appPort
let app
async function get$(path, query) {
const html = await renderViaHTTP(appPort, path, query)
return cheerio.load(html)
}
describe('TypeScript Features', () => {
describe('default behavior', () => {
let output = ''
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort, {
onStderr(msg) {
output += msg || ''
},
onStdout(msg) {
output += msg || ''
},
})
})
afterAll(() => killApp(app))
it('should alias components', async () => {
const $ = await get$('/basic-alias')
expect($('body').text()).toMatch(/World/)
})
it('should resolve the first item in the array first', async () => {
const $ = await get$('/resolve-order')
expect($('body').text()).toMatch(/Hello from a/)
})
it('should resolve the second item as fallback', async () => {
const $ = await get$('/resolve-fallback')
expect($('body').text()).toMatch(/Hello from only b/)
})
it('should resolve a single matching alias', async () => {
const $ = await get$('/single-alias')
expect($('body').text()).toMatch(/Hello/)
})
it('should resolve a wildcard alias', async () => {
const $ = await get$('/wildcard-alias')
expect($('body').text()).toMatch(/world/)
})
it('should have correct module not found error', async () => {
const basicPage = join(appDir, 'pages/basic-alias.js')
const contents = await fs.readFile(basicPage, 'utf8')
await fs.writeFile(basicPage, contents.replace('@c/world', '@c/worldd'))
await renderViaHTTP(appPort, '/basic-alias')
const found = await check(
() => output,
/Module not found: Can't resolve '@c\/worldd'/,
false
)
await fs.writeFile(basicPage, contents)
expect(found).toBe(true)
})
})
describe('should build', () => {
beforeAll(async () => {
await nextBuild(appDir)
})
it('should trace correctly', async () => {
const singleAliasTrace = await fs.readJSON(
join(appDir, '.next/server/pages/single-alias.js.nft.json')
)
const wildcardAliasTrace = await fs.readJSON(
join(appDir, '.next/server/pages/wildcard-alias.js.nft.json')
)
const resolveOrderTrace = await fs.readJSON(
join(appDir, '.next/server/pages/resolve-order.js.nft.json')
)
const resolveFallbackTrace = await fs.readJSON(
join(appDir, '.next/server/pages/resolve-fallback.js.nft.json')
)
const basicAliasTrace = await fs.readJSON(
join(appDir, '.next/server/pages/basic-alias.js.nft.json')
)
expect(
singleAliasTrace.files.some((file) =>
file.includes('components/hello.js')
)
).toBe(true)
expect(
wildcardAliasTrace.files.some((file) =>
file.includes('mypackage/myfile.js')
)
).toBe(true)
expect(
resolveOrderTrace.files.some((file) => file.includes('lib/a/api.js'))
).toBe(true)
expect(
resolveFallbackTrace.files.some((file) =>
file.includes('lib/b/b-only.js')
)
).toBe(true)
expect(
basicAliasTrace.files.some((file) =>
file.includes('components/world.js')
)
).toBe(true)
})
})
})