@@ -65,28 +65,35 @@ const TEST_TIMEOUT = parseInt(process.env.TEST_TIMEOUT || '5000', 10);
6565
6666test . beforeEach ( async ( { page } ) => {
6767 await page . goto ( 'http://localhost:3000/test-page.html' ) ;
68+ await page . waitForLoadState ( 'networkidle' ) ;
6869} ) ;
6970
7071for ( const tag of TAGS ) {
7172 for ( let idx = 0 ; idx < TEST_ITERATIONS ; idx ++ ) {
72- test ( `${ tag } hydration iteration ${ idx + 1 } ` , async ( { page } ) => {
73- await page . evaluate ( ( ) => window . gc ?.( ) ) ;
74-
75- const duration = await page . evaluate (
76- async ( { tag, timeout, idx } ) => {
73+ test ( `${ tag } hydration (${ idx + 1 } )` , async ( { page } ) => {
74+ const { hydratedTime } = await page . evaluate (
75+ async ( { tag, timeout } ) => {
76+ window . gc ?.( ) ;
7777 await customElements . whenDefined ( tag ) ;
78-
7978 const el = document . createElement ( tag ) ;
80- el . setAttribute ( 'data-test' , `hydration-${ idx } ` ) ;
81- const start = performance . now ( ) ;
8279 document . body . appendChild ( el ) ;
80+ const start = performance . now ( ) ;
81+ let hydratedTime : number | null = null ;
82+ let themedTime : number | null = null ;
8383
8484 await new Promise < void > ( ( resolve ) => {
8585 let cleaned = false ;
8686
8787 const timeoutId = setTimeout ( cleanup , timeout ) ;
88+
8889 const observer = new MutationObserver ( ( ) => {
89- if ( el . classList . contains ( 'hydrated' ) ) cleanup ( ) ;
90+ if ( ! hydratedTime && el . classList . contains ( 'hydrated' ) ) {
91+ hydratedTime = performance . now ( ) - start ;
92+ }
93+ if ( hydratedTime && el . hasAttribute ( 'data-themed' ) ) {
94+ themedTime = performance . now ( ) - hydratedTime ;
95+ cleanup ( ) ;
96+ }
9097 } ) ;
9198
9299 function cleanup ( ) {
@@ -100,24 +107,28 @@ for (const tag of TAGS) {
100107
101108 observer . observe ( el , {
102109 attributes : true ,
103- attributeFilter : [ 'class' ] ,
110+ attributeFilter : [ 'class' , 'data-themed' ] ,
104111 } ) ;
105112 } ) ;
106113
107- const end = performance . now ( ) ;
108- return end - start ;
114+ return {
115+ hydratedTime,
116+ themedTime,
117+ } ;
109118 } ,
110- { tag, timeout : TEST_TIMEOUT , idx } ,
119+ { tag, timeout : TEST_TIMEOUT } ,
111120 ) ;
112121
113- if ( ! results . has ( tag ) ) {
114- results . set ( tag , {
115- name : tag ,
116- values : [ ] ,
117- unit : 'ms' ,
118- } ) ;
122+ if ( hydratedTime !== null ) {
123+ if ( ! results . has ( tag ) ) {
124+ results . set ( tag , {
125+ name : tag ,
126+ values : [ ] ,
127+ unit : 'ms' ,
128+ } ) ;
129+ }
130+ results . get ( tag ) ! . values . push ( Math . round ( hydratedTime ) ) ;
119131 }
120- results . get ( tag ) ! . values . push ( Math . round ( duration ) ) ;
121132 } ) ;
122133 }
123134}
0 commit comments