/
index.test.js
173 lines (154 loc) · 4.71 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import cheerio from 'cheerio'
import * as path from 'path'
import {
renderViaHTTP,
findPort,
launchApp,
nextBuild,
killApp,
check,
File,
} from 'next-test-utils'
import * as JSON5 from 'json5'
const appDir = join(__dirname, '..')
let appPort
let app
async function get$(path, query) {
const html = await renderViaHTTP(appPort, path, query)
return cheerio.load(html)
}
function runTests() {
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 appTrace = await fs.readJSON(
join(appDir, '.next/server/pages/_app.js.nft.json')
)
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(
appTrace.files.some((file) => file.includes('node_modules/next'))
).toBe(true)
expect(
singleAliasTrace.files.some((file) =>
file.includes('components/hello.js')
)
).toBe(false)
expect(
wildcardAliasTrace.files.some((file) =>
file.includes('mypackage/myfile.js')
)
).toBe(true)
expect(
wildcardAliasTrace.files.some((file) =>
file.includes('mypackage/data.js')
)
).toBe(false)
expect(
resolveOrderTrace.files.some((file) => file.includes('lib/a/api.js'))
).toBe(false)
expect(
resolveOrderTrace.files.some((file) =>
file.includes('mypackage/data.js')
)
).toBe(true)
expect(
resolveFallbackTrace.files.some((file) =>
file.includes('lib/b/b-only.js')
)
).toBe(false)
expect(
basicAliasTrace.files.some((file) =>
file.includes('components/world.js')
)
).toBe(false)
})
})
}
describe('jsconfig paths', () => {
runTests()
})
const jsconfig = new File(path.resolve(__dirname, '../jsconfig.json'))
describe('jsconfig paths without baseurl', () => {
beforeAll(() => {
const jsconfigContent = JSON5.parse(jsconfig.originalContent)
delete jsconfigContent.compilerOptions.baseUrl
jsconfigContent.compilerOptions.paths = {
'@c/*': ['./components/*'],
'@lib/*': ['./lib/a/*', './lib/b/*'],
'@mycomponent': ['./components/hello.js'],
'*': ['./node_modules/*'],
}
jsconfig.write(JSON.stringify(jsconfigContent, null, 2))
})
afterAll(() => {
jsconfig.restore()
})
runTests()
})