vite/playground/test-utils.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

328 lines
8.6 KiB
TypeScript
Raw Normal View History

2020-12-20 03:33:13 +00:00
// test utils used in e2e tests for playgrounds.
2022-05-11 09:03:19 +00:00
// `import { getColor } from '~utils'`
2020-12-20 03:33:13 +00:00
import fs from 'node:fs'
import path from 'node:path'
2020-12-20 03:33:13 +00:00
import colors from 'css-color-names'
import type { ConsoleMessage, ElementHandle } from 'playwright-chromium'
2022-03-30 06:56:20 +00:00
import type { Manifest } from 'vite'
import { normalizePath } from 'vite'
2022-03-28 12:41:00 +00:00
import { fromComment } from 'convert-source-map'
2022-05-11 06:33:20 +00:00
import { expect } from 'vitest'
import type { ExecaChildProcess } from 'execa'
import { isBuild, isWindows, page, testDir } from './vitestSetup'
2022-05-11 09:03:19 +00:00
export * from './vitestSetup'
2022-05-11 09:03:19 +00:00
2022-04-04 18:33:48 +00:00
// make sure these ports are unique
export const ports = {
cli: 9510,
'cli-module': 9511,
'legacy/ssr': 9520,
lib: 9521,
'optimize-missing-deps': 9522,
'legacy/client-and-ssr': 9523,
'assets/url-base': 9524, // not imported but used in `assets/vite.config-url-base.js`
ssr: 9600,
'ssr-deps': 9601,
'ssr-html': 9602,
'ssr-noexternal': 9603,
'ssr-pug': 9604,
'ssr-webworker': 9605,
2022-04-04 18:33:48 +00:00
'css/postcss-caching': 5005,
'css/postcss-plugins-different-dir': 5006,
'css/dynamic-import': 5007,
2022-04-04 18:33:48 +00:00
}
export const hmrPorts = {
'optimize-missing-deps': 24680,
ssr: 24681,
'ssr-deps': 24682,
'ssr-html': 24683,
'ssr-noexternal': 24684,
'ssr-pug': 24685,
}
2022-04-04 18:33:48 +00:00
2020-12-20 03:33:13 +00:00
const hexToNameMap: Record<string, string> = {}
Object.keys(colors).forEach((color) => {
hexToNameMap[colors[color]] = color
})
function componentToHex(c: number): string {
const hex = c.toString(16)
2021-07-13 15:05:08 +00:00
return hex.length === 1 ? '0' + hex : hex
2020-12-20 03:33:13 +00:00
}
function rgbToHex(rgb: string): string {
const match = rgb.match(/rgb\((\d+),\s*(\d+),\s*(\d+)\)/)
if (match) {
const [_, rs, gs, bs] = match
return (
'#' +
componentToHex(parseInt(rs, 10)) +
componentToHex(parseInt(gs, 10)) +
componentToHex(parseInt(bs, 10))
)
} else {
return '#000000'
}
2020-12-20 03:33:13 +00:00
}
const timeout = (n: number) => new Promise((r) => setTimeout(r, n))
async function toEl(el: string | ElementHandle): Promise<ElementHandle> {
if (typeof el === 'string') {
const realEl = await page.$(el)
if (realEl == null) {
throw new Error(`Cannot find element: "${el}"`)
}
return realEl
2020-12-20 03:33:13 +00:00
}
return el
}
export async function getColor(el: string | ElementHandle): Promise<string> {
2020-12-20 03:33:13 +00:00
el = await toEl(el)
const rgb = await el.evaluate((el) => getComputedStyle(el as Element).color)
return hexToNameMap[rgbToHex(rgb)] ?? rgb
2020-12-20 03:33:13 +00:00
}
export async function getBg(el: string | ElementHandle): Promise<string> {
2020-12-21 23:37:04 +00:00
el = await toEl(el)
return el.evaluate((el) => getComputedStyle(el as Element).backgroundImage)
}
export async function getBgColor(el: string | ElementHandle): Promise<string> {
el = await toEl(el)
return el.evaluate((el) => getComputedStyle(el as Element).backgroundColor)
}
export function readFile(filename: string): string {
return fs.readFileSync(path.resolve(testDir, filename), 'utf-8')
}
export function editFile(
filename: string,
replacer: (str: string) => string,
runInBuild: boolean = false,
): void {
if (isBuild && !runInBuild) return
filename = path.resolve(testDir, filename)
2020-12-20 03:33:13 +00:00
const content = fs.readFileSync(filename, 'utf-8')
const modified = replacer(content)
fs.writeFileSync(filename, modified)
}
export function addFile(filename: string, content: string): void {
fs.writeFileSync(path.resolve(testDir, filename), content)
2021-01-20 22:45:41 +00:00
}
export function removeFile(filename: string): void {
fs.unlinkSync(path.resolve(testDir, filename))
2021-01-20 22:45:41 +00:00
}
export function listAssets(base = ''): string[] {
const assetsDir = path.join(testDir, 'dist', base, 'assets')
return fs.readdirSync(assetsDir)
}
2022-05-18 05:07:41 +00:00
export function findAssetFile(
match: string | RegExp,
base = '',
assets = 'assets',
): string {
const assetsDir = path.join(testDir, 'dist', base, assets)
let files: string[]
try {
files = fs.readdirSync(assetsDir)
} catch (e) {
if (e.code === 'ENOENT') {
return ''
}
throw e
}
2020-12-30 23:47:35 +00:00
const file = files.find((file) => {
return file.match(match)
})
return file ? fs.readFileSync(path.resolve(assetsDir, file), 'utf-8') : ''
}
export function readManifest(base = ''): Manifest {
return JSON.parse(
fs.readFileSync(path.join(testDir, 'dist', base, 'manifest.json'), 'utf-8'),
)
}
2020-12-20 03:33:13 +00:00
/**
* Poll a getter until the value it returns includes the expected value.
*/
export async function untilUpdated(
2020-12-23 04:07:57 +00:00
poll: () => string | Promise<string>,
expected: string | RegExp,
2021-03-26 21:54:28 +00:00
runInBuild = false,
): Promise<void> {
2021-03-26 21:54:28 +00:00
if (isBuild && !runInBuild) return
const maxTries = process.env.CI ? 200 : 50
2020-12-20 03:33:13 +00:00
for (let tries = 0; tries < maxTries; tries++) {
const actual = (await poll()) ?? ''
if (
(typeof expected === 'string'
? actual.indexOf(expected) > -1
: actual.match(expected)) ||
tries === maxTries - 1
) {
2020-12-20 03:33:13 +00:00
expect(actual).toMatch(expected)
break
} else {
await timeout(50)
}
}
}
/**
* Retry `func` until it does not throw error.
*/
export async function withRetry(
func: () => Promise<void>,
runInBuild = false,
): Promise<void> {
if (isBuild && !runInBuild) return
const maxTries = process.env.CI ? 200 : 50
for (let tries = 0; tries < maxTries; tries++) {
try {
await func()
return
} catch {}
await timeout(50)
}
await func()
}
type UntilBrowserLogAfterCallback = (logs: string[]) => PromiseLike<void> | void
export async function untilBrowserLogAfter(
operation: () => any,
target: string | RegExp | Array<string | RegExp>,
expectOrder?: boolean,
callback?: UntilBrowserLogAfterCallback,
): Promise<string[]>
export async function untilBrowserLogAfter(
operation: () => any,
target: string | RegExp | Array<string | RegExp>,
callback?: UntilBrowserLogAfterCallback,
): Promise<string[]>
export async function untilBrowserLogAfter(
operation: () => any,
target: string | RegExp | Array<string | RegExp>,
arg3?: boolean | UntilBrowserLogAfterCallback,
arg4?: UntilBrowserLogAfterCallback,
): Promise<string[]> {
const expectOrder = typeof arg3 === 'boolean' ? arg3 : false
const callback = typeof arg3 === 'boolean' ? arg4 : arg3
const promise = untilBrowserLog(target, expectOrder)
await operation()
const logs = await promise
if (callback) {
await callback(logs)
}
return logs
}
async function untilBrowserLog(
target?: string | RegExp | Array<string | RegExp>,
expectOrder = true,
): Promise<string[]> {
let resolve: () => void
let reject: (reason: any) => void
const promise = new Promise<void>((_resolve, _reject) => {
resolve = _resolve
reject = _reject
})
const logs = []
try {
const isMatch = (matcher: string | RegExp) => (text: string) =>
typeof matcher === 'string' ? text === matcher : matcher.test(text)
let processMsg: (text: string) => boolean
if (!target) {
processMsg = () => true
} else if (Array.isArray(target)) {
if (expectOrder) {
const remainingTargets = [...target]
processMsg = (text: string) => {
const nextTarget = remainingTargets.shift()
expect(text).toMatch(nextTarget)
return remainingTargets.length === 0
}
} else {
const remainingMatchers = target.map(isMatch)
processMsg = (text: string) => {
const nextIndex = remainingMatchers.findIndex((matcher) =>
matcher(text),
)
if (nextIndex >= 0) {
remainingMatchers.splice(nextIndex, 1)
}
return remainingMatchers.length === 0
}
}
} else {
processMsg = isMatch(target)
}
const handleMsg = (msg: ConsoleMessage) => {
try {
const text = msg.text()
logs.push(text)
const done = processMsg(text)
if (done) {
resolve()
}
} catch (err) {
reject(err)
}
}
page.on('console', handleMsg)
} catch (err) {
reject(err)
}
await promise
return logs
}
export const extractSourcemap = (content: string): any => {
2022-03-28 12:41:00 +00:00
const lines = content.trim().split('\n')
return fromComment(lines[lines.length - 1]).toObject()
}
export const formatSourcemapForSnapshot = (map: any): any => {
const root = normalizePath(testDir)
2022-03-28 12:41:00 +00:00
const m = { ...map }
delete m.file
delete m.names
m.sources = m.sources.map((source) => source.replace(root, '/root'))
return m
}
// helper function to kill process, uses taskkill on windows to ensure child process is killed too
export async function killProcess(
serverProcess: ExecaChildProcess,
): Promise<void> {
if (isWindows) {
try {
2022-06-13 18:26:27 +00:00
const { execaCommandSync } = await import('execa')
execaCommandSync(`taskkill /pid ${serverProcess.pid} /T /F`)
} catch (e) {
console.error('failed to taskkill:', e)
}
} else {
serverProcess.kill('SIGTERM', { forceKillAfterTimeout: 2000 })
}
}