forked from underfin/vite-plugin-vue2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
214 lines (188 loc) · 6.42 KB
/
util.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
import fs from 'fs-extra'
import path from 'path'
import execa from 'execa'
import puppeteer, { ElementHandle } from 'puppeteer'
let devServer: any
let browser: puppeteer.Browser
let page: puppeteer.Page
let binPath: string
const fixtureDir = path.join(__dirname, '../playground')
const tempDir = path.join(__dirname, '../temp')
export async function preTest() {
try {
await fs.remove(tempDir)
} catch (e) {}
await fs.copy(fixtureDir, tempDir, {
filter: (file) => !/dist|node_modules/.test(file),
})
await execa('yarn', { cwd: tempDir })
binPath = path.resolve(tempDir, './node_modules/vite/bin/vite.js')
await build()
}
async function build() {
console.log('building...')
const buildOutput = await execa(binPath, ['build'], {
cwd: tempDir,
})
expect(buildOutput.stderr).toBe('')
console.log('build complete. running build tests...')
}
export async function postTest() {
try {
await fs.remove(tempDir)
} catch (e) {}
}
export async function startServer(isBuild: boolean) {
// start dev server
devServer = execa(binPath, {
cwd: isBuild ? path.join(tempDir, '/dist') : tempDir,
})
browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
})
await new Promise((resolve) => {
devServer.stdout.on('data', (data: Buffer) => {
if (data.toString().match('running')) {
console.log('dev server running.')
resolve('')
}
})
})
console.log('launching browser')
page = await browser.newPage()
await page.goto('http://localhost:3000')
}
export async function killServer() {
if (browser) await browser.close()
if (devServer) {
devServer.kill('SIGTERM', {
forceKillAfterTimeout: 2000,
})
}
}
const getEl = async (selectorOrEl: string | ElementHandle) => {
return typeof selectorOrEl === 'string'
? await page.$(selectorOrEl)
: selectorOrEl
}
const getText = async (selectorOrEl: string | ElementHandle) => {
const el = await getEl(selectorOrEl)
return el ? el.evaluate((el) => el.textContent) : null
}
const getComputedColor = async (selectorOrEl: string | ElementHandle) => {
return (await getEl(selectorOrEl))!.evaluate(
// @ts-ignore
(el) => getComputedStyle(el).color
)
}
const timeout = (n: number) => new Promise((r) => setTimeout(r, n))
async function updateFile(file: string, replacer: (content: string) => void) {
const compPath = path.join(tempDir, file)
const content = await fs.readFile(compPath, 'utf-8')
await fs.writeFile(compPath, replacer(content))
}
// poll until it updates
async function expectByPolling(poll: () => Promise<any>, expected: any) {
const maxTries = 100
for (let tries = 0; tries < maxTries; tries++) {
const actual = (await poll()) || ''
if (actual.indexOf(expected) > -1 || tries === maxTries - 1) {
expect(actual).toMatch(expected)
break
} else {
await timeout(50)
}
}
}
export function declareTests(isBuild: boolean) {
if (!isBuild) {
test('hmr (vue re-render)', async () => {
const button = await page.$('.hmr-increment')
await button!.click()
expect(await getText(button!)).toMatch('>>> 1 <<<')
await updateFile('hmr/TestHmr.vue', (content) =>
content.replace('{{ count }}', 'count is {{ count }}')
)
// note: using the same button to ensure the component did only re-render
// if it's a reload, it would have replaced the button with a new one.
await expectByPolling(() => getText(button!), 'count is 1')
})
test('hmr (vue reload)', async () => {
await updateFile('hmr/TestHmr.vue', (content) =>
content.replace('count: 0', 'count: 1337')
)
await expectByPolling(() => getText('.hmr-increment'), 'count is 1337')
})
}
// if (!isBuild) {
// test('hmr (style removal)', async () => {
// await updateFile('css/TestPostCss.vue', (content) =>
// content.replace(/<style>(.|\s)*<\/style>/, ``)
// )
// await expectByPolling(
// () => getComputedColor('.postcss-from-sfc'),
// 'rgb(0, 0, 0)'
// )
// })
// }
test('SFC <style scoped>', async () => {
const el = await page.$('.style-scoped')
expect(await getComputedColor(el!)).toBe('rgb(138, 43, 226)')
if (!isBuild) {
await updateFile('css/TestScopedCss.vue', (content) =>
content.replace('rgb(138, 43, 226)', 'rgb(0, 0, 0)')
)
await expectByPolling(() => getComputedColor(el!), 'rgb(0, 0, 0)')
}
})
test('SFC <style module>', async () => {
const el = await page.$('.css-modules-sfc')
expect(await getComputedColor(el!)).toBe('rgb(0, 0, 255)')
if (!isBuild) {
await updateFile('css/TestCssModules.vue', (content) =>
content.replace('color: blue;', 'color: rgb(0, 0, 0);')
)
// css module results in component reload so must use fresh selector
await expectByPolling(
() => getComputedColor('.css-modules-sfc'),
'rgb(0, 0, 0)'
)
}
})
test('SFC <custom>', async () => {
expect(await getText('.custom-block')).toMatch('Custom Block')
expect(await getText('.custom-block-lang')).toMatch('Custom Block')
expect(await getText('.custom-block-src')).toMatch('Custom Block')
})
test('SFC src imports', async () => {
expect(await getText('.src-imports-script')).toMatch('src="./script.ts"')
const el = await getEl('.src-imports-style')
expect(await getComputedColor(el!)).toBe('rgb(119, 136, 153)')
if (!isBuild) {
// test style first, should not reload the component
await updateFile('src-import/style.css', (c) =>
c.replace('rgb(119, 136, 153)', 'rgb(0, 0, 0)')
)
await expectByPolling(() => getComputedColor(el!), 'rgb(0, 0, 0)')
// script
await updateFile('src-import/script.ts', (c) => c.replace('hello', 'bye'))
await expectByPolling(() => getText('.src-imports-script'), 'bye from')
// template
// todo fix test, file change is only triggered one event.
// src/node/server/serverPluginHmr.ts is not triggered, maybe caused by chokidar
// await updateFile('src-import/template.html', (c) =>
// c.replace('gray', 'red')
// )
// await expectByPolling(
// () => getText('.src-imports-style'),
// 'This should be light red'
// )
}
})
test('Jsx', async () => {
expect(await getText('.jsx')).toMatch('JSX works!')
})
test('JsxSFC', async () => {
expect(await getText('.jsx-sfc')).toMatch('JSX & SFC works!')
})
}