-
-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathutils.ts
More file actions
120 lines (114 loc) · 2.95 KB
/
utils.ts
File metadata and controls
120 lines (114 loc) · 2.95 KB
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
import { type Locator, type Page, expect } from '@playwright/test'
import { readFileSync, writeFileSync } from 'node:fs'
import {
build,
createServer,
loadConfigFromFile,
mergeConfig,
preview,
} from 'vite'
export const setupWaitForLogs = async (page: Page) => {
let logs: string[] = []
page.on('console', (log) => {
logs.push(log.text())
})
return (...messages: (string | RegExp)[]) =>
expect
.poll(() => {
if (
messages.every((m) =>
typeof m === 'string'
? logs.includes(m)
: logs.some((l) => m.test(l)),
)
) {
logs = []
return true
}
return logs
})
.toBe(true)
}
let port = 5173
export const setupDevServer = async (name: string) => {
process.env['NODE_ENV'] = 'development'
const root = `playground-temp/${name}`
const res = await loadConfigFromFile(
{ command: 'serve', mode: 'development' },
undefined,
root,
)
const testConfig = mergeConfig(res!.config, {
root,
logLevel: 'silent',
configFile: false,
server: { port: port++ },
})
const server = await (await createServer(testConfig)).listen()
return {
testUrl: `http://localhost:${server.config.server.port}${server.config.base}`,
server,
editFile: (
name: string,
...replacements: [searchValue: string, replaceValue: string][]
) => {
const path = `${root}/${name}`
let content = readFileSync(path, 'utf-8')
for (const [search, replace] of replacements) {
if (!content.includes(search)) {
throw new Error(`'${search}' not found in ${name}`)
}
content = content.replace(search, replace)
}
writeFileSync(path, content)
},
}
}
export const setupBuildAndPreview = async (name: string) => {
process.env['NODE_ENV'] = 'production'
const root = `playground-temp/${name}`
const res = await loadConfigFromFile(
{ command: 'build', mode: 'production' },
undefined,
root,
)
const testConfig = mergeConfig(
{ root, logLevel: 'silent', configFile: false, preview: { port: port++ } },
res!.config,
)
await build(testConfig)
const server = await preview(testConfig)
return {
testUrl: server.resolvedUrls!.local[0],
server,
}
}
export const expectColor = async (
locator: Locator,
property: 'color' | 'backgroundColor',
color: string,
) => {
await expect
.poll(async () =>
rgbToHex(
await locator.evaluate(
(el, prop) => getComputedStyle(el)[prop],
property,
),
),
)
.toBe(color)
}
const rgbToHex = (rgb: string): string => {
const [_, rs, gs, bs] = rgb.match(/rgb\((\d+),\s*(\d+),\s*(\d+)\)/)!
return (
'#' +
componentToHex(parseInt(rs, 10)) +
componentToHex(parseInt(gs, 10)) +
componentToHex(parseInt(bs, 10))
)
}
const componentToHex = (c: number): string => {
const hex = c.toString(16)
return hex.length === 1 ? '0' + hex : hex
}