-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
html.spec.ts
258 lines (215 loc) · 7.35 KB
/
html.spec.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { beforeAll, describe, expect, test } from 'vitest'
import {
browserLogs,
editFile,
getColor,
isBuild,
isServe,
page,
viteTestUrl
} from '~utils'
function testPage(isNested: boolean) {
test('pre transform', async () => {
expect(await page.$('head meta[name=viewport]')).toBeTruthy()
})
test('string transform', async () => {
expect(await page.textContent('h1')).toBe(
isNested ? 'Nested' : 'Transformed'
)
})
test('tags transform', async () => {
const el = await page.$('head meta[name=description]')
expect(await el.getAttribute('content')).toBe('a vite app')
const kw = await page.$('head meta[name=keywords]')
expect(await kw.getAttribute('content')).toBe('es modules')
})
test('combined transform', async () => {
expect(await page.title()).toBe('Test HTML transforms')
// the p should be injected to body
expect(await page.textContent('body p.inject')).toBe('This is injected')
})
test('server only transform', async () => {
if (!isBuild) {
expect(await page.textContent('body p.server')).toMatch(
'injected only during dev'
)
} else {
expect(await page.innerHTML('body')).not.toMatch('p class="server"')
}
})
test('build only transform', async () => {
if (isBuild) {
expect(await page.textContent('body p.build')).toMatch(
'injected only during build'
)
} else {
expect(await page.innerHTML('body')).not.toMatch('p class="build"')
}
})
test('conditional transform', async () => {
if (isNested) {
expect(await page.textContent('body p.conditional')).toMatch(
'injected only for /nested/'
)
} else {
expect(await page.innerHTML('body')).not.toMatch('p class="conditional"')
}
})
test('body prepend/append transform', async () => {
expect(await page.innerHTML('body')).toMatch(
/prepended to body(.*)appended to body/s
)
})
test('css', async () => {
expect(await getColor('h1')).toBe(isNested ? 'red' : 'blue')
expect(await getColor('p')).toBe('grey')
})
if (isNested) {
test('relative path in html asset', async () => {
expect(await page.textContent('.relative-js')).toMatch('hello')
expect(await getColor('.relative-css')).toMatch('red')
})
}
}
describe('main', () => {
testPage(false)
test('preserve comments', async () => {
const html = await page.innerHTML('body')
expect(html).toMatch(`<!-- comment one -->`)
expect(html).toMatch(`<!-- comment two -->`)
})
})
describe('nested', () => {
beforeAll(async () => {
await page.goto(viteTestUrl + '/nested/')
})
testPage(true)
})
describe('nested w/ query', () => {
beforeAll(async () => {
await page.goto(viteTestUrl + '/nested/index.html?v=1')
})
testPage(true)
})
describe.runIf(isBuild)('build', () => {
describe('scriptAsync', () => {
beforeAll(async () => {
await page.goto(viteTestUrl + '/scriptAsync.html')
})
test('script is async', async () => {
expect(await page.$('head script[type=module][async]')).toBeTruthy()
expect(await page.$('head script[type=module]:not([async])')).toBeNull()
})
})
describe('scriptMixed', () => {
beforeAll(async () => {
await page.goto(viteTestUrl + '/scriptMixed.html')
})
test('script is mixed', async () => {
expect(await page.$('head script[type=module][async]')).toBeNull()
expect(await page.$('head script[type=module]:not([async])')).toBeTruthy()
})
})
describe('zeroJS', () => {
// Ensure that the modulePreload polyfill is discarded in this case
beforeAll(async () => {
await page.goto(viteTestUrl + '/zeroJS.html')
})
test('zeroJS', async () => {
expect(await page.$('head script[type=module]')).toBeNull()
})
})
describe('inline entry', () => {
const _countTags = (selector) => page.$$eval(selector, (t) => t.length)
const countScriptTags = _countTags.bind(this, 'script[type=module]')
const countPreloadTags = _countTags.bind(this, 'link[rel=modulepreload]')
test('is inlined', async () => {
await page.goto(viteTestUrl + '/inline/shared-1.html?v=1')
expect(await countScriptTags()).toBeGreaterThan(1)
expect(await countPreloadTags()).toBe(0)
})
test('is not inlined', async () => {
await page.goto(viteTestUrl + '/inline/unique.html?v=1')
expect(await countScriptTags()).toBe(1)
expect(await countPreloadTags()).toBeGreaterThan(0)
})
test('execution order when inlined', async () => {
await page.goto(viteTestUrl + '/inline/shared-2.html?v=1')
expect((await page.textContent('#output')).trim()).toBe(
'dep1 common dep2 dep3 shared'
)
})
test('execution order when not inlined', async () => {
await page.goto(viteTestUrl + '/inline/unique.html?v=1')
expect((await page.textContent('#output')).trim()).toBe(
'dep1 common dep2 unique'
)
})
})
})
describe('noHead', () => {
beforeAll(async () => {
await page.goto(viteTestUrl + '/noHead.html')
})
test('noHead tags injection', async () => {
const el = await page.$('html meta[name=description]')
expect(await el.getAttribute('content')).toBe('a vite app')
const kw = await page.$('html meta[name=keywords]')
expect(await kw.getAttribute('content')).toBe('es modules')
})
})
describe('noBody', () => {
beforeAll(async () => {
await page.goto(viteTestUrl + '/noBody.html')
})
test('noBody tags injection', async () => {
// this selects the first noscript in body, even without a body tag
const el = await page.$('body noscript')
expect(await el.innerHTML()).toMatch(`<!-- this is prepended to body -->`)
const kw = await page.$('html:last-child')
expect(await kw.innerHTML()).toMatch(`<!-- this is appended to body -->`)
})
})
describe('Unicode path', () => {
test('direct access', async () => {
await page.goto(
viteTestUrl + '/unicode-path/中文-にほんご-한글-🌕🌖🌗/index.html'
)
expect(await page.textContent('h1')).toBe('Unicode path')
})
test('spa fallback', async () => {
await page.goto(viteTestUrl + '/unicode-path/中文-にほんご-한글-🌕🌖🌗/')
expect(await page.textContent('h1')).toBe('Unicode path')
})
})
describe('link with props', () => {
test('separate links with different media props', async () => {
await page.goto(viteTestUrl + '/link-props/index.html')
expect(await getColor('h1')).toBe('red')
})
})
describe.runIf(isServe)('invalid', () => {
test('should be 500 with overlay', async () => {
const response = await page.goto(viteTestUrl + '/invalid.html')
expect(response.status()).toBe(500)
const errorOverlay = await page.waitForSelector('vite-error-overlay')
expect(errorOverlay).toBeTruthy()
const message = await errorOverlay.$$eval('.message-body', (m) => {
return m[0].innerHTML
})
expect(message).toMatch(/^Unable to parse HTML/)
})
test('should reload when fixed', async () => {
await page.goto(viteTestUrl + '/invalid.html')
await editFile('invalid.html', (content) => {
return content.replace('<div Bad', '<div> Good')
})
const content = await page.waitForSelector('text=Good HTML')
expect(content).toBeTruthy()
})
})
test('importmap', () => {
expect(browserLogs).not.toContain(
'An import map is added after module script load was triggered.'
)
})