@@ -19,6 +19,7 @@ import type { FullResult, IncrementalResult } from '../../../application/results
19
19
import { writeTestFiles } from '../../../karma/application_builder' ;
20
20
import { NormalizedUnitTestBuilderOptions } from '../../options' ;
21
21
import type { TestExecutor } from '../api' ;
22
+ import { setupBrowserConfiguration } from './browser-provider' ;
22
23
23
24
type VitestCoverageOption = Exclude < InlineConfig [ 'coverage' ] , undefined > ;
24
25
@@ -171,89 +172,6 @@ export class VitestExecutor implements TestExecutor {
171
172
}
172
173
}
173
174
174
- function findBrowserProvider (
175
- projectResolver : NodeJS . RequireResolve ,
176
- ) : import ( 'vitest/node' ) . BrowserBuiltinProvider | undefined {
177
- // One of these must be installed in the project to use browser testing
178
- const vitestBuiltinProviders = [ 'playwright' , 'webdriverio' ] as const ;
179
-
180
- for ( const providerName of vitestBuiltinProviders ) {
181
- try {
182
- projectResolver ( providerName ) ;
183
-
184
- return providerName ;
185
- } catch { }
186
- }
187
-
188
- return undefined ;
189
- }
190
-
191
- function normalizeBrowserName ( browserName : string ) : string {
192
- // Normalize browser names to match Vitest's expectations for headless but also supports karma's names
193
- // e.g., 'ChromeHeadless' -> 'chrome', 'FirefoxHeadless' -> 'firefox'
194
- // and 'Chrome' -> 'chrome', 'Firefox' -> 'firefox'.
195
- const normalized = browserName . toLowerCase ( ) ;
196
-
197
- return normalized . replace ( / h e a d l e s s $ / , '' ) ;
198
- }
199
-
200
- function setupBrowserConfiguration (
201
- browsers : string [ ] | undefined ,
202
- debug : boolean ,
203
- projectSourceRoot : string ,
204
- ) : { browser ?: import ( 'vitest/node' ) . BrowserConfigOptions ; errors ?: string [ ] } {
205
- if ( browsers === undefined ) {
206
- return { } ;
207
- }
208
-
209
- const projectResolver = createRequire ( projectSourceRoot + '/' ) . resolve ;
210
- let errors : string [ ] | undefined ;
211
-
212
- try {
213
- projectResolver ( '@vitest/browser' ) ;
214
- } catch {
215
- errors ??= [ ] ;
216
- errors . push (
217
- 'The "browsers" option requires the "@vitest/browser" package to be installed within the project.' +
218
- ' Please install this package and rerun the test command.' ,
219
- ) ;
220
- }
221
-
222
- const provider = findBrowserProvider ( projectResolver ) ;
223
- if ( ! provider ) {
224
- errors ??= [ ] ;
225
- errors . push (
226
- 'The "browsers" option requires either "playwright" or "webdriverio" to be installed within the project.' +
227
- ' Please install one of these packages and rerun the test command.' ,
228
- ) ;
229
- }
230
-
231
- // Vitest current requires the playwright browser provider to use the inspect-brk option used by "debug"
232
- if ( debug && provider !== 'playwright' ) {
233
- errors ??= [ ] ;
234
- errors . push (
235
- 'Debugging browser mode tests currently requires the use of "playwright".' +
236
- ' Please install this package and rerun the test command.' ,
237
- ) ;
238
- }
239
-
240
- if ( errors ) {
241
- return { errors } ;
242
- }
243
-
244
- const browser = {
245
- enabled : true ,
246
- provider,
247
- headless : browsers . some ( ( name ) => name . toLowerCase ( ) . includes ( 'headless' ) ) ,
248
-
249
- instances : browsers . map ( ( browserName ) => ( {
250
- browser : normalizeBrowserName ( browserName ) ,
251
- } ) ) ,
252
- } ;
253
-
254
- return { browser } ;
255
- }
256
-
257
175
function generateOutputPath ( ) : string {
258
176
const datePrefix = new Date ( ) . toISOString ( ) . replaceAll ( / [ - : . ] / g, '' ) ;
259
177
const uuidSuffix = randomUUID ( ) . slice ( 0 , 8 ) ;
0 commit comments