/
hmr.spec.ts
163 lines (140 loc) · 5.08 KB
/
hmr.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
import { isBuild, editFile, untilUpdated } from '../../testUtils'
test('should render', async () => {
expect(await page.textContent('.app')).toBe('1')
expect(await page.textContent('.dep')).toBe('1')
expect(await page.textContent('.nested')).toBe('1')
})
if (!isBuild) {
test('should connect', async () => {
expect(browserLogs.length).toBe(2)
expect(browserLogs.some((msg) => msg.match('connected'))).toBe(true)
browserLogs.length = 0
})
test('self accept', async () => {
const el = await page.$('.app')
editFile('hmr.js', (code) => code.replace('const foo = 1', 'const foo = 2'))
await untilUpdated(() => el.textContent(), '2')
expect(browserLogs).toMatchObject([
'>>> vite:beforeUpdate -- update',
'foo was: 1',
'(self-accepting 1) foo is now: 2',
'(self-accepting 2) foo is now: 2',
'[vite] hot updated: /hmr.js'
])
browserLogs.length = 0
editFile('hmr.js', (code) => code.replace('const foo = 2', 'const foo = 3'))
await untilUpdated(() => el.textContent(), '3')
expect(browserLogs).toMatchObject([
'>>> vite:beforeUpdate -- update',
'foo was: 2',
'(self-accepting 1) foo is now: 3',
'(self-accepting 2) foo is now: 3',
'[vite] hot updated: /hmr.js'
])
browserLogs.length = 0
})
test('accept dep', async () => {
const el = await page.$('.dep')
editFile('hmrDep.js', (code) =>
code.replace('const foo = 1', 'const foo = 2')
)
await untilUpdated(() => el.textContent(), '2')
expect(browserLogs).toMatchObject([
'>>> vite:beforeUpdate -- update',
'(dep) foo was: 1',
'(dep) foo from dispose: 1',
'(single dep) foo is now: 2',
'(single dep) nested foo is now: 1',
'(multi deps) foo is now: 2',
'(multi deps) nested foo is now: 1',
'[vite] hot updated: /hmrDep.js via /hmr.js'
])
browserLogs.length = 0
editFile('hmrDep.js', (code) =>
code.replace('const foo = 2', 'const foo = 3')
)
await untilUpdated(() => el.textContent(), '3')
expect(browserLogs).toMatchObject([
'>>> vite:beforeUpdate -- update',
'(dep) foo was: 2',
'(dep) foo from dispose: 2',
'(single dep) foo is now: 3',
'(single dep) nested foo is now: 1',
'(multi deps) foo is now: 3',
'(multi deps) nested foo is now: 1',
'[vite] hot updated: /hmrDep.js via /hmr.js'
])
browserLogs.length = 0
})
test('nested dep propagation', async () => {
const el = await page.$('.nested')
editFile('hmrNestedDep.js', (code) =>
code.replace('const foo = 1', 'const foo = 2')
)
await untilUpdated(() => el.textContent(), '2')
expect(browserLogs).toMatchObject([
'>>> vite:beforeUpdate -- update',
'(dep) foo was: 3',
'(dep) foo from dispose: 3',
'(single dep) foo is now: 3',
'(single dep) nested foo is now: 2',
'(multi deps) foo is now: 3',
'(multi deps) nested foo is now: 2',
'[vite] hot updated: /hmrDep.js via /hmr.js'
])
browserLogs.length = 0
editFile('hmrNestedDep.js', (code) =>
code.replace('const foo = 2', 'const foo = 3')
)
await untilUpdated(() => el.textContent(), '3')
expect(browserLogs).toMatchObject([
'>>> vite:beforeUpdate -- update',
'(dep) foo was: 3',
'(dep) foo from dispose: 3',
'(single dep) foo is now: 3',
'(single dep) nested foo is now: 3',
'(multi deps) foo is now: 3',
'(multi deps) nested foo is now: 3',
'[vite] hot updated: /hmrDep.js via /hmr.js'
])
browserLogs.length = 0
})
test('plugin hmr handler + custom event', async () => {
const el = await page.$('.custom')
editFile('customFile.js', (code) => code.replace('custom', 'edited'))
await untilUpdated(() => el.textContent(), 'edited')
})
test('plugin client-server communication', async () => {
const el = await page.$('.custom-communication')
await untilUpdated(() => el.textContent(), '3')
})
test('full-reload encodeURI path', async () => {
await page.goto(
viteTestUrl + '/unicode-path/中文-にほんご-한글-🌕🌖🌗/index.html'
)
const el = await page.$('#app')
expect(await el.textContent()).toBe('title')
await editFile(
'unicode-path/中文-にほんご-한글-🌕🌖🌗/index.html',
(code) => code.replace('title', 'title2')
)
await page.waitForEvent('load')
await untilUpdated(
async () => (await page.$('#app')).textContent(),
'title2'
)
})
test('CSS update preserves query params', async () => {
await page.goto(viteTestUrl)
editFile('global.css', (code) => code.replace('white', 'tomato'))
const elprev = await page.$('.css-prev')
const elpost = await page.$('.css-post')
await untilUpdated(() => elprev.textContent(), 'param=required')
await untilUpdated(() => elpost.textContent(), 'param=required')
const textprev = await elprev.textContent()
const textpost = await elpost.textContent()
expect(textprev).not.toBe(textpost)
expect(textprev).not.toMatch('direct')
expect(textpost).not.toMatch('direct')
})
}