1
1
// test utils used in e2e tests for playgrounds.
2
- // this can be directly imported in any playground tests as 'testUtils', e.g.
3
- // `import { getColor } from 'testUtils'`
2
+ // `import { getColor } from '~utils'`
4
3
5
- import fs from 'fs'
6
- import path from 'path'
7
- import colors from 'css-color-names'
8
- import { ElementHandle } from 'playwright-chromium'
9
- import type { Manifest } from 'vite'
10
-
11
- export function slash ( p : string ) : string {
12
- return p . replace ( / \\ / g, '/' )
13
- }
14
-
15
- export const isBuild = ! ! process . env . VITE_TEST_BUILD
16
-
17
- const testPath = expect . getState ( ) . testPath
18
- const testName = slash ( testPath ) . match ( / p l a y g r o u n d \/ ( [ \w - ] + ) \/ / ) ?. [ 1 ]
19
- export const testDir = path . resolve ( __dirname , '../../temp' , testName )
20
-
21
- const hexToNameMap : Record < string , string > = { }
22
- Object . keys ( colors ) . forEach ( ( color ) => {
23
- hexToNameMap [ colors [ color ] ] = color
24
- } )
25
-
26
- function componentToHex ( c : number ) : string {
27
- const hex = c . toString ( 16 )
28
- return hex . length === 1 ? '0' + hex : hex
29
- }
30
-
31
- function rgbToHex ( rgb : string ) : string {
32
- const match = rgb . match ( / r g b \( ( \d + ) , \s * ( \d + ) , \s * ( \d + ) \) / )
33
- if ( match ) {
34
- const [ _ , rs , gs , bs ] = match
35
- return (
36
- '#' +
37
- componentToHex ( parseInt ( rs , 10 ) ) +
38
- componentToHex ( parseInt ( gs , 10 ) ) +
39
- componentToHex ( parseInt ( bs , 10 ) )
40
- )
41
- } else {
42
- return '#000000'
43
- }
44
- }
45
-
46
- const timeout = ( n : number ) => new Promise ( ( r ) => setTimeout ( r , n ) )
47
-
48
- async function toEl ( el : string | ElementHandle ) : Promise < ElementHandle > {
49
- if ( typeof el === 'string' ) {
50
- return await page . $ ( el )
51
- }
52
- return el
53
- }
54
-
55
- export async function getColor ( el : string | ElementHandle ) : Promise < string > {
56
- el = await toEl ( el )
57
- const rgb = await el . evaluate ( ( el ) => getComputedStyle ( el as Element ) . color )
58
- return hexToNameMap [ rgbToHex ( rgb ) ] || rgb
59
- }
60
-
61
- export async function getBg ( el : string | ElementHandle ) : Promise < string > {
62
- el = await toEl ( el )
63
- return el . evaluate ( ( el ) => getComputedStyle ( el as Element ) . backgroundImage )
64
- }
65
-
66
- export function readFile ( filename : string ) : string {
67
- return fs . readFileSync ( path . resolve ( testDir , filename ) , 'utf-8' )
68
- }
69
-
70
- export function editFile (
71
- filename : string ,
72
- replacer : ( str : string ) => string ,
73
- runInBuild : boolean = false
74
- ) : void {
75
- if ( isBuild && ! runInBuild ) return
76
- filename = path . resolve ( testDir , filename )
77
- const content = fs . readFileSync ( filename , 'utf-8' )
78
- const modified = replacer ( content )
79
- fs . writeFileSync ( filename , modified )
80
- }
81
-
82
- export function addFile ( filename : string , content : string ) : void {
83
- fs . writeFileSync ( path . resolve ( testDir , filename ) , content )
84
- }
85
-
86
- export function removeFile ( filename : string ) : void {
87
- fs . unlinkSync ( path . resolve ( testDir , filename ) )
88
- }
89
-
90
- export function listAssets ( base = '' ) : string [ ] {
91
- const assetsDir = path . join ( testDir , 'dist' , base , 'assets' )
92
- return fs . readdirSync ( assetsDir )
93
- }
94
-
95
- export function findAssetFile ( match : string | RegExp , base = '' ) : string {
96
- const assetsDir = path . join ( testDir , 'dist' , base , 'assets' )
97
- const files = fs . readdirSync ( assetsDir )
98
- const file = files . find ( ( file ) => {
99
- return file . match ( match )
100
- } )
101
- return file ? fs . readFileSync ( path . resolve ( assetsDir , file ) , 'utf-8' ) : ''
102
- }
103
-
104
- export function readManifest ( base = '' ) : Manifest {
105
- return JSON . parse (
106
- fs . readFileSync ( path . join ( testDir , 'dist' , base , 'manifest.json' ) , 'utf-8' )
107
- )
108
- }
109
-
110
- /**
111
- * Poll a getter until the value it returns includes the expected value.
112
- */
113
- export async function untilUpdated (
114
- poll : ( ) => string | Promise < string > ,
115
- expected : string ,
116
- runInBuild = false
117
- ) : Promise < void > {
118
- if ( isBuild && ! runInBuild ) return
119
- const maxTries = process . env . CI ? 100 : 50
120
- for ( let tries = 0 ; tries < maxTries ; tries ++ ) {
121
- const actual = ( await poll ( ) ) || ''
122
- if ( actual . indexOf ( expected ) > - 1 || tries === maxTries - 1 ) {
123
- expect ( actual ) . toMatch ( expected )
124
- break
125
- } else {
126
- await timeout ( 50 )
127
- }
128
- }
129
- }
130
-
131
- /**
132
- * Send the rebuild complete message in build watch
133
- */
134
- export { notifyRebuildComplete } from '../../scripts/jestPerTestSetup'
4
+ export * from './vitestSetup'
0 commit comments