-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
html.spec.ts
130 lines (109 loc) · 3.75 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
import { getColor, isBuild } from '../../testUtils'
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')
})
}
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 () => {
// viteTestUrl is globally injected in scripts/jestPerTestSetup.ts
await page.goto(viteTestUrl + '/nested/')
})
testPage(true)
})
describe('nested w/ query', () => {
beforeAll(async () => {
// viteTestUrl is globally injected in scripts/jestPerTestSetup.ts
await page.goto(viteTestUrl + '/nested/index.html?v=1')
})
testPage(true)
})
if (isBuild) {
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'
)
})
})
}