@@ -338,61 +338,29 @@ async function loadResultFile(file: ResultFile): Promise<string> {
338338 return readFile ( file . source , 'utf-8' ) ;
339339}
340340
341- function rebasePath ( filePath : string , fromRoot : string , toRoot : string ) : string {
342- if ( path . isAbsolute ( filePath ) ) {
343- const relativePath = path . relative ( fromRoot , filePath ) ;
344- if (
345- relativePath !== '..' &&
346- ! relativePath . startsWith ( '..' + path . sep ) &&
347- ! path . isAbsolute ( relativePath )
348- ) {
349- return path . join ( toRoot , relativePath ) ;
350- }
351- }
352-
353- return filePath ;
354- }
355-
356341export function createVitestPlugins ( pluginOptions : PluginOptions ) : Vite . Plugin [ ] {
357- const { workspaceRoot, buildResultFiles, testFileToEntryPoint, setupFiles } = pluginOptions ;
342+ const { buildResultFiles, setupFiles } = pluginOptions ;
343+ let { workspaceRoot, testFileToEntryPoint } = pluginOptions ;
358344 const isWindows = platform ( ) === 'win32' ;
359- const setupFileSet = new Set (
345+ let setupFileSet = new Set (
360346 setupFiles . map ( ( file ) =>
361347 toPosixPath ( path . isAbsolute ( file ) ? file : path . join ( workspaceRoot , file ) ) ,
362348 ) ,
363349 ) ;
364- let vitestConfig : ResolvedConfig | undefined ;
365-
366- const normalizeToWorkspace = ( filePath : string ) : string =>
367- vitestConfig ?. root ? rebasePath ( filePath , vitestConfig . root , workspaceRoot ) : filePath ;
368-
369- const normalizeToVitestRoot = ( filePath : string ) : string =>
370- toPosixPath (
371- vitestConfig ?. root ? rebasePath ( filePath , workspaceRoot , vitestConfig . root ) : filePath ,
372- ) ;
350+ let vitestConfig : ResolvedConfig ;
373351
374352 /** The build output path served for a module, whether it is a test entry point or a built artifact. */
375353 const getOutputPath = ( id : string ) : string => {
376- const normalizedId = toPosixPath ( normalizeToWorkspace ( id ) ) ;
377- const entryPoint = testFileToEntryPoint . get ( normalizedId ) ;
354+ const entryPoint = testFileToEntryPoint . get ( id ) ;
378355
379- return entryPoint
380- ? entryPoint + '.js'
381- : toPosixPath ( path . relative ( workspaceRoot , normalizedId ) ) ;
356+ return entryPoint ? entryPoint + '.js' : toPosixPath ( path . relative ( workspaceRoot , id ) ) ;
382357 } ;
383358
384359 // Setup files must not be wrapped in a virtual import stub because Vitest only invalidates
385360 // the setup file itself between test files; wrapping it would cause the underlying bundle
386361 // to be cached, preventing per-test hooks from running on subsequent test files.
387- const isCoverageRedirect = ( id : string ) : boolean => {
388- const normalizedId = toPosixPath ( normalizeToWorkspace ( id ) ) ;
389-
390- return (
391- ! ! vitestConfig ?. coverage ?. enabled &&
392- testFileToEntryPoint . has ( normalizedId ) &&
393- ! setupFileSet . has ( normalizedId )
394- ) ;
395- } ;
362+ const isCoverageRedirect = ( id : string ) : boolean =>
363+ ! ! vitestConfig ?. coverage ?. enabled && testFileToEntryPoint . has ( id ) && ! setupFileSet . has ( id ) ;
396364
397365 return [
398366 {
@@ -401,6 +369,29 @@ export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[]
401369 configureVitest ( context : VitestPluginContext ) {
402370 vitestConfig = context . vitest . config ;
403371
372+ if ( vitestConfig . root && vitestConfig . root !== toPosixPath ( workspaceRoot ) ) {
373+ const previousRoot = workspaceRoot ;
374+ workspaceRoot = vitestConfig . root ;
375+ testFileToEntryPoint = new Map (
376+ Array . from ( testFileToEntryPoint , ( [ file , entryPoint ] ) => [
377+ file . startsWith ( 'angular:' )
378+ ? file
379+ : toPosixPath ( path . join ( workspaceRoot , path . relative ( previousRoot , file ) ) ) ,
380+ entryPoint ,
381+ ] ) ,
382+ ) ;
383+ setupFileSet = new Set (
384+ setupFiles . map ( ( file ) =>
385+ toPosixPath (
386+ path . join (
387+ workspaceRoot ,
388+ path . isAbsolute ( file ) ? path . relative ( previousRoot , file ) : file ,
389+ ) ,
390+ ) ,
391+ ) ,
392+ ) ;
393+ }
394+
404395 // Vitest keys its file system module cache on the file found on disk. A test entry point is
405396 // served from its bundle instead, which also holds the application code it imports, so the
406397 // key must follow the served build output, or the cache would return a stale bundle.
@@ -432,7 +423,7 @@ export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[]
432423 resolveId : ( id : string , importer : string | undefined ) => {
433424 // Fast path for test entry points.
434425 if ( testFileToEntryPoint . has ( id ) ) {
435- return normalizeToVitestRoot ( id ) ;
426+ return id ;
436427 }
437428
438429 // Workaround for Vitest in Windows when a fully qualified absolute path is provided with
@@ -446,12 +437,9 @@ export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[]
446437 }
447438 }
448439
449- id = normalizeToWorkspace ( id ) ;
450-
451440 // Determine the base directory for resolution.
452441 let baseDir : string ;
453442 if ( importer ) {
454- importer = toPosixPath ( normalizeToWorkspace ( importer ) ) ;
455443 // If the importer is a test entry point, resolve relative to the workspace root.
456444 // Otherwise, resolve relative to the importer's directory.
457445 baseDir = testFileToEntryPoint . has ( importer ) ? workspaceRoot : path . dirname ( importer ) ;
@@ -474,13 +462,13 @@ export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[]
474462 fullPath = toPosixPath ( fullPath ) ;
475463
476464 if ( testFileToEntryPoint . has ( fullPath ) ) {
477- return normalizeToVitestRoot ( fullPath ) ;
465+ return fullPath ;
478466 }
479467
480468 // Check if the resolved path corresponds to a known build artifact.
481469 const relativePath = path . relative ( workspaceRoot , fullPath ) ;
482470 if ( buildResultFiles . has ( toPosixPath ( relativePath ) ) ) {
483- return normalizeToVitestRoot ( fullPath ) ;
471+ return fullPath ;
484472 }
485473
486474 // If the module cannot be resolved from the build artifacts, let other plugins handle it.
@@ -509,7 +497,7 @@ export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[]
509497
510498 const map = sourceMapText ? JSON . parse ( sourceMapText ) : undefined ;
511499 if ( map ) {
512- adjustSourcemapSources ( map , true , workspaceRoot , toPosixPath ( normalizeToWorkspace ( id ) ) ) ;
500+ adjustSourcemapSources ( map , true , workspaceRoot , id ) ;
513501 }
514502
515503 return {
0 commit comments