-
-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathvue.spec.ts
289 lines (251 loc) · 8.89 KB
/
vue.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { describe, expect, test } from 'vitest'
import {
browserLogs,
editFile,
getBg,
getColor,
isBuild,
page,
serverLogs,
untilUpdated,
} from '~utils'
test('should render', async () => {
expect(await page.textContent('h1')).toMatch('Vue SFCs')
})
test('should update', async () => {
expect(await page.textContent('.hmr-inc')).toMatch('count is 0')
await page.click('.hmr-inc')
expect(await page.textContent('.hmr-inc')).toMatch('count is 1')
})
test('template/script latest syntax support', async () => {
expect(await page.textContent('.syntax')).toBe('baz')
})
test('import ts with .js extension with lang="ts"', async () => {
expect(await page.textContent('.ts-import')).toBe('success')
expect(await page.textContent('.ts-import2')).toBe('success')
})
test('should remove comments in prod', async () => {
expect(await page.innerHTML('.comments')).toBe(isBuild ? `` : `<!--hello-->`)
})
test(':slotted', async () => {
expect(await getColor('.slotted')).toBe('red')
})
describe('dep scan', () => {
test('scan deps from <script setup lang="ts">', async () => {
expect(await page.textContent('.scan')).toBe('ok')
})
test('find deps on initial scan', () => {
serverLogs.forEach((log) => {
expect(log).not.toMatch('new dependencies found')
})
})
})
describe('pre-processors', () => {
test('pug', async () => {
expect(await page.textContent('p.pug')).toMatch(
`This is rendered from <template lang="pug">`,
)
// #1383 pug default doctype
expect(await page.textContent('.pug-slot')).toMatch(`slot content`)
editFile('PreProcessors.vue', (code) =>
code.replace('Pre-Processors', 'Updated'),
)
await untilUpdated(() => page.textContent('h2.pre-processors'), 'Updated')
})
test('scss', async () => {
const el = await page.$('p.pug')
expect(await getColor(el)).toBe('magenta')
editFile('PreProcessors.vue', (code) =>
code.replace('$color: magenta;', '$color: red;'),
)
await untilUpdated(() => getColor(el), 'red')
})
test('less + scoped', async () => {
const el = await page.$('p.pug-less')
expect(await getColor(el)).toBe('green')
editFile('PreProcessors.vue', (code) =>
code.replace('@color: green;', '@color: blue;'),
)
await untilUpdated(() => getColor(el), 'blue')
})
test('stylus + change lang', async () => {
expect(await getColor('p.pug-stylus')).toBe('orange')
editFile('PreProcessors.vue', (code) =>
code
.replace('<style lang="stylus">', '<style lang="scss">')
.replace('color = orange', '$color: yellow;')
.replace('color: color', '{ color: $color; }'),
)
await untilUpdated(() => getColor('p.pug-stylus'), 'yellow')
editFile('PreProcessors.vue', (code) =>
code.replace('$color: yellow;', '$color: orange;'),
)
await untilUpdated(() => getColor('p.pug-stylus'), 'orange')
})
})
describe('css modules', () => {
test('basic', async () => {
expect(await getColor('.sfc-css-modules')).toBe('blue')
editFile('CssModules.vue', (code) =>
code.replace('color: blue;', 'color: red;'),
)
await untilUpdated(() => getColor('.sfc-css-modules'), 'red')
})
test('with preprocessor + name', async () => {
expect(await getColor('.sfc-css-modules-with-pre')).toBe('orange')
editFile('CssModules.vue', (code) =>
code.replace('color: orange;', 'color: blue;'),
)
await untilUpdated(() => getColor('.sfc-css-modules-with-pre'), 'blue')
})
})
describe('asset reference', () => {
const assetMatch = isBuild
? /\/assets\/asset-\w{8}\.png/
: '/assets/asset.png'
test('should not 404', () => {
browserLogs.forEach((msg) => {
expect(msg).not.toMatch('404')
})
})
test('relative import', async () => {
const el = await page.$('img.relative-import')
expect(await el.evaluate((el) => (el as HTMLImageElement).src)).toMatch(
assetMatch,
)
})
test('absolute import', async () => {
const el = await page.$('img.absolute-import')
expect(await el.evaluate((el) => (el as HTMLImageElement).src)).toMatch(
assetMatch,
)
})
test('absolute import from public dir', async () => {
const el = await page.$('img.public-import')
expect(await el.evaluate((el) => (el as HTMLImageElement).src)).toMatch(
`/icon.png`,
)
})
test('svg fragment', async () => {
const img = await page.$('.svg-frag')
expect(await img.getAttribute('src')).toMatch(/svg#icon-heart-view$/)
})
test('relative url from <style>', async () => {
const assetMatch = isBuild
? /\/assets\/asset-\w{8}\.png/
: '/assets/asset.png'
expect(await getBg('.relative-style-url')).toMatch(assetMatch)
})
})
describe('hmr', () => {
test('should re-render and preserve state when template is edited', async () => {
editFile('Hmr.vue', (code) => code.replace('HMR', 'HMR updated'))
await untilUpdated(() => page.textContent('h2.hmr'), 'HMR updated')
expect(await page.textContent('.hmr-inc')).toMatch('count is 1')
})
test('should update style and preserve state when style is edited', async () => {
expect(await getColor('.hmr-inc')).toBe('red')
editFile('Hmr.vue', (code) => code.replace('color: red;', 'color: blue;'))
await untilUpdated(() => getColor('.hmr-inc'), 'blue')
expect(await page.textContent('.hmr-inc')).toMatch('count is 1')
})
test('should reload and reset state when script is edited', async () => {
editFile('Hmr.vue', (code) =>
code.replace('let foo: number = 0', 'let foo: number = 100'),
)
await untilUpdated(() => page.textContent('.hmr-inc'), 'count is 100')
})
test('global hmr for some scenarios', async () => {
editFile('Hmr.vue', (code) =>
code.replace('</template>', ' <Node/>\n' + '</template>'),
)
await untilUpdated(() => page.innerHTML('.node'), 'this is node')
})
test('should re-render when template is emptied', async () => {
editFile('Hmr.vue', (code) => code.replace(/<template>.+<\/template>/s, ''))
await untilUpdated(() => page.innerHTML('.hmr-block'), '<!---->')
})
test('should re-render when template and tsx script both changed', async () => {
editFile('HmrTsx.vue', (code) => code.replace(/count/g, 'updatedCount'))
await untilUpdated(
() => page.innerHTML('.hmr-tsx-block .hmr-tsx-inc'),
'updatedCount is 0',
)
})
})
describe('src imports', () => {
test('script src with ts', async () => {
expect(await page.textContent('.src-imports-script')).toMatch(
'hello from script src',
)
editFile('src-import/script.ts', (code) =>
code.replace('hello from script src', 'updated'),
)
await untilUpdated(() => page.textContent('.src-imports-script'), 'updated')
})
test('style src', async () => {
const el = await page.$('.src-imports-style')
expect(await getColor(el)).toBe('tan')
editFile('src-import/style.css', (code) =>
code.replace('color: tan', 'color: red'),
)
await untilUpdated(() => getColor(el), 'red')
})
test('template src import hmr', async () => {
const el = await page.$('.src-imports-style')
editFile('src-import/template.html', (code) =>
code.replace('should be tan', 'should be red'),
)
await untilUpdated(() => el.textContent(), 'should be red')
})
})
describe('custom blocks', () => {
test('should work', async () => {
expect(await page.textContent('.custom-block')).toMatch('こんにちは')
})
})
describe('async component', () => {
test('should work', async () => {
expect(await page.textContent('.async-component')).toMatch('ab == ab')
})
})
describe('ref transform', () => {
test('should work', async () => {
expect(await page.textContent('.ref-transform')).toMatch('0')
await page.click('.ref-transform')
expect(await page.textContent('.ref-transform')).toMatch('1')
})
})
describe('custom element', () => {
test('should work', async () => {
await page.click('.custom-element')
expect(await page.textContent('.custom-element')).toMatch('count: 2')
expect(await getColor('.custom-element')).toBe('green')
})
})
describe('setup import template', () => {
test('should work', async () => {
expect(await page.textContent('.setup-import-template')).toMatch('0')
await page.click('.setup-import-template')
expect(await page.textContent('.setup-import-template')).toMatch('1')
})
})
describe('vue worker', () => {
test('should work', async () => {
expect(await page.textContent('.vue-worker')).toMatch('worker load!')
})
})
describe('import with ?url', () => {
test('should work', async () => {
expect(await page.textContent('.import-with-url-query')).toMatch(
isBuild ? /^data:/ : '/Null.vue',
)
})
})
describe('ts expression in the pug template', () => {
test('should work', async () => {
expect(await page.textContent('.ts-expression')).toMatch('0')
await page.click('.ts-expression')
expect(await page.textContent('.ts-expression')).toMatch('1')
})
})