@@ -38,12 +38,10 @@ function createIframe(container: HTMLDivElement, file: string) {
3838 iframe . setAttribute ( 'src' , `${ url . pathname } __vitest_test__/__test__/${ encodeURIComponent ( file ) } ` )
3939 iframe . setAttribute ( 'data-vitest' , 'true' )
4040
41- const config = getConfig ( ) . browser
42- iframe . style . width = `${ config . viewport . width } px`
43- iframe . style . height = `${ config . viewport . height } px`
44-
4541 iframe . style . display = 'block'
4642 iframe . style . border = 'none'
43+ iframe . style . zIndex = '1'
44+ iframe . style . position = 'relative'
4745 iframe . setAttribute ( 'allowfullscreen' , 'true' )
4846 iframe . setAttribute ( 'allow' , 'clipboard-write;' )
4947
@@ -71,8 +69,8 @@ interface IframeErrorEvent {
7169
7270interface IframeViewportEvent {
7371 type : 'viewport'
74- width : number | string
75- height : number | string
72+ width : number
73+ height : number
7674 id : string
7775}
7876
@@ -111,8 +109,6 @@ client.ws.addEventListener('open', async () => {
111109 switch ( e . data . type ) {
112110 case 'viewport' : {
113111 const { width, height, id } = e . data
114- const widthStr = typeof width === 'number' ? `${ width } px` : width
115- const heightStr = typeof height === 'number' ? `${ height } px` : height
116112 const iframe = iframes . get ( id )
117113 if ( ! iframe ) {
118114 const error = new Error ( `Cannot find iframe with id ${ id } ` )
@@ -123,13 +119,7 @@ client.ws.addEventListener('open', async () => {
123119 } , 'Teardown Error' )
124120 return
125121 }
126- iframe . style . width = widthStr
127- iframe . style . height = heightStr
128- const ui = getUiAPI ( )
129- if ( ui ) {
130- await new Promise ( r => requestAnimationFrame ( r ) )
131- ui . recalculateDetailPanels ( )
132- }
122+ await setIframeViewport ( iframe , width , height )
133123 channel . postMessage ( { type : 'viewport:done' , id } )
134124 break
135125 }
@@ -143,7 +133,7 @@ client.ws.addEventListener('open', async () => {
143133 // so we only select it when the run is done
144134 if ( ui && filenames . length > 1 ) {
145135 const id = generateFileId ( filenames [ filenames . length - 1 ] )
146- ui . setCurrentById ( id )
136+ ui . setCurrentFileId ( id )
147137 }
148138 await done ( )
149139 }
@@ -189,37 +179,26 @@ async function createTesters(testFiles: string[]) {
189179 container . className = 'scrolls'
190180 container . textContent = ''
191181 }
182+ const { width, height } = config . browser . viewport
192183
193184 if ( config . isolate === false ) {
194- createIframe (
185+ const iframe = createIframe (
195186 container ,
196187 ID_ALL ,
197188 )
198189
199- const ui = getUiAPI ( )
200-
201- if ( ui ) {
202- await new Promise ( r => requestAnimationFrame ( r ) )
203- ui . recalculateDetailPanels ( )
204- }
190+ await setIframeViewport ( iframe , width , height )
205191 }
206192 else {
207193 // otherwise, we need to wait for each iframe to finish before creating the next one
208194 // this is the most stable way to run tests in the browser
209195 for ( const file of testFiles ) {
210- const ui = getUiAPI ( )
211-
212- createIframe (
196+ const iframe = createIframe (
213197 container ,
214198 file ,
215199 )
216200
217- if ( ui ) {
218- const id = generateFileId ( file )
219- ui . setCurrentById ( id )
220- await new Promise ( r => requestAnimationFrame ( r ) )
221- ui . recalculateDetailPanels ( )
222- }
201+ await setIframeViewport ( iframe , width , height )
223202
224203 await new Promise < void > ( ( resolve ) => {
225204 channel . addEventListener ( 'message' , function handler ( e : MessageEvent < IframeChannelEvent > ) {
@@ -240,3 +219,14 @@ function generateFileId(file: string) {
240219 const path = relative ( config . root , file )
241220 return generateHash ( `${ path } ${ project } ` )
242221}
222+
223+ async function setIframeViewport ( iframe : HTMLIFrameElement , width : number , height : number ) {
224+ const ui = getUiAPI ( )
225+ if ( ui ) {
226+ await ui . setIframeViewport ( width , height )
227+ }
228+ else {
229+ iframe . style . width = `${ width } px`
230+ iframe . style . height = `${ height } px`
231+ }
232+ }
0 commit comments