1+ import path from 'node:path' ;
12import { pathToFileURL } from 'node:url' ;
23import { defineConfig } from 'vite' ;
34import { beforeEach , describe , expect , it , vi } from 'vitest' ;
@@ -26,6 +27,18 @@ vi.mock('./vitest-tsconfig-path-aliases.js', () => ({
2627const MOCK_PROJECT_ROOT_STRING = '/Users/test/project' ;
2728const MOCK_PROJECT_ROOT_URL = pathToFileURL ( `${ MOCK_PROJECT_ROOT_STRING } /` ) ;
2829
30+ // Cross-platform path helpers to match what the actual code generates
31+ const mockPath = ( ...segments : string [ ] ) =>
32+ path . resolve ( MOCK_PROJECT_ROOT_STRING , ...segments ) ;
33+ const mockCacheDir = ( name : string ) => mockPath ( 'node_modules' , '.vite' , name ) ;
34+ const mockVitestCacheDir = ( ) => mockPath ( 'node_modules' , '.vitest' ) ;
35+ const mockGlobalSetup = ( ) => mockPath ( 'global-setup.ts' ) ;
36+ const mockReportsDir = ( projectKey : string , kind : string ) =>
37+ kind === 'e2e'
38+ ? mockPath ( 'e2e' , projectKey , '.coverage' )
39+ : mockPath ( 'packages' , projectKey , '.coverage' , `${ kind } -tests` ) ;
40+ const mockSetupFile = mockPath ;
41+
2942const TEST_TIMEOUTS = {
3043 SHORT : 5000 ,
3144 MEDIUM : 10_000 ,
@@ -61,23 +74,23 @@ describe('createVitestConfig', () => {
6174
6275 expect ( config ) . toEqual (
6376 expect . objectContaining ( {
64- cacheDir : ` ${ MOCK_PROJECT_ROOT_STRING } /node_modules/.vite/ test-package` ,
77+ cacheDir : mockCacheDir ( ' test-package' ) ,
6578 test : expect . objectContaining ( {
6679 reporters : [ 'basic' ] ,
6780 globals : true ,
6881 cache : {
69- dir : ` ${ MOCK_PROJECT_ROOT_STRING } /node_modules/.vitest` ,
82+ dir : mockVitestCacheDir ( ) ,
7083 } ,
7184 alias : expect . any ( Object ) ,
7285 pool : 'threads' ,
7386 poolOptions : { threads : { singleThread : true } } ,
7487 environment : 'node' ,
7588 include : EXPECTED_INCLUDES . unit ,
76- globalSetup : [ ` ${ MOCK_PROJECT_ROOT_STRING } /global-setup.ts` ] ,
89+ globalSetup : [ mockGlobalSetup ( ) ] ,
7790 setupFiles : [ ] ,
7891 coverage : expect . objectContaining ( {
7992 reporter : [ 'text' , 'lcov' ] ,
80- reportsDirectory : ` ${ MOCK_PROJECT_ROOT_STRING } /packages/ test-package/.coverage/ unit-tests` ,
93+ reportsDirectory : mockReportsDir ( ' test-package' , ' unit' ) ,
8194 exclude : DEFAULT_EXCLUDES ,
8295 } ) ,
8396 } ) ,
@@ -98,10 +111,10 @@ describe('createVitestConfig', () => {
98111
99112 expect ( config ) . toEqual (
100113 expect . objectContaining ( {
101- cacheDir : ` ${ MOCK_PROJECT_ROOT_STRING } /node_modules/.vite/ test-package` ,
114+ cacheDir : mockCacheDir ( ' test-package' ) ,
102115 test : expect . objectContaining ( {
103116 include : EXPECTED_INCLUDES . unit ,
104- globalSetup : [ ` ${ MOCK_PROJECT_ROOT_STRING } /global-setup.ts` ] ,
117+ globalSetup : [ mockGlobalSetup ( ) ] ,
105118 } ) ,
106119 } ) ,
107120 ) ;
@@ -148,9 +161,9 @@ describe('createVitestConfig', () => {
148161 expect . objectContaining ( {
149162 test : expect . objectContaining ( {
150163 include : EXPECTED_INCLUDES . int ,
151- globalSetup : [ ` ${ MOCK_PROJECT_ROOT_STRING } /global-setup.ts` ] ,
164+ globalSetup : [ mockGlobalSetup ( ) ] ,
152165 coverage : expect . objectContaining ( {
153- reportsDirectory : ` ${ MOCK_PROJECT_ROOT_STRING } /packages/ test-package/.coverage/ int-tests` ,
166+ reportsDirectory : mockReportsDir ( ' test-package' , ' int' ) ,
154167 } ) ,
155168 } ) ,
156169 } ) ,
@@ -202,7 +215,7 @@ describe('createVitestConfig', () => {
202215 globalSetup : undefined ,
203216 coverage : expect . objectContaining ( {
204217 reporter : [ 'text' , 'lcov' ] ,
205- reportsDirectory : ` ${ MOCK_PROJECT_ROOT_STRING } /e2e/ test-package/.coverage` ,
218+ reportsDirectory : mockReportsDir ( ' test-package' , 'e2e' ) ,
206219 exclude : DEFAULT_EXCLUDES ,
207220 } ) ,
208221 } ) ,
@@ -224,7 +237,7 @@ describe('createVitestConfig', () => {
224237
225238 expect ( config ) . toEqual (
226239 expect . objectContaining ( {
227- cacheDir : ` ${ MOCK_PROJECT_ROOT_STRING } /node_modules/.vite/ custom-cache-key` ,
240+ cacheDir : mockCacheDir ( ' custom-cache-key' ) ,
228241 } ) ,
229242 ) ;
230243 } ) ;
@@ -240,7 +253,7 @@ describe('createVitestConfig', () => {
240253
241254 expect ( config ) . toEqual (
242255 expect . objectContaining ( {
243- cacheDir : ` ${ MOCK_PROJECT_ROOT_STRING } /node_modules/.vite/ test-package` ,
256+ cacheDir : mockCacheDir ( ' test-package' ) ,
244257 } ) ,
245258 ) ;
246259 } ) ;
@@ -265,7 +278,7 @@ describe('createVitestConfig', () => {
265278 expect ( config ) . toEqual (
266279 expect . objectContaining ( {
267280 test : expect . objectContaining ( {
268- setupFiles : [ ` ${ MOCK_PROJECT_ROOT_STRING } / setup.ts` ] ,
281+ setupFiles : [ mockSetupFile ( ' setup.ts' ) ] ,
269282 } ) ,
270283 } ) ,
271284 ) ;
@@ -290,8 +303,8 @@ describe('createVitestConfig', () => {
290303 expect . objectContaining ( {
291304 test : expect . objectContaining ( {
292305 setupFiles : [
293- ` ${ MOCK_PROJECT_ROOT_STRING } / setup1.ts` ,
294- ` ${ MOCK_PROJECT_ROOT_STRING } / setup2.ts` ,
306+ mockSetupFile ( ' setup1.ts' ) ,
307+ mockSetupFile ( ' setup2.ts' ) ,
295308 ] ,
296309 } ) ,
297310 } ) ,
@@ -317,9 +330,9 @@ describe('createVitestConfig', () => {
317330 expect . objectContaining ( {
318331 test : expect . objectContaining ( {
319332 setupFiles : [
320- ` ${ MOCK_PROJECT_ROOT_STRING } / setup1.ts` ,
321- ` ${ MOCK_PROJECT_ROOT_STRING } / setup2.ts` ,
322- ` ${ MOCK_PROJECT_ROOT_STRING } / setup3.ts` ,
333+ mockSetupFile ( ' setup1.ts' ) ,
334+ mockSetupFile ( ' setup2.ts' ) ,
335+ mockSetupFile ( ' setup3.ts' ) ,
323336 ] ,
324337 } ) ,
325338 } ) ,
@@ -392,7 +405,7 @@ describe('createVitestConfig', () => {
392405 test : expect . objectContaining ( {
393406 coverage : expect . objectContaining ( {
394407 reporter : [ 'text' , 'lcov' ] ,
395- reportsDirectory : ` ${ MOCK_PROJECT_ROOT_STRING } /packages/ test-package/.coverage/ unit-tests` ,
408+ reportsDirectory : mockReportsDir ( ' test-package' , ' unit' ) ,
396409 exclude : [ ...DEFAULT_EXCLUDES , 'custom/**' , 'ignore/**' ] ,
397410 } ) ,
398411 } ) ,
@@ -422,7 +435,7 @@ describe('createVitestConfig', () => {
422435 test : expect . objectContaining ( {
423436 coverage : expect . objectContaining ( {
424437 reporter : [ 'text' , 'lcov' ] ,
425- reportsDirectory : ` ${ MOCK_PROJECT_ROOT_STRING } /packages/ test-package/.coverage/ unit-tests` ,
438+ reportsDirectory : mockReportsDir ( ' test-package' , ' unit' ) ,
426439 exclude : DEFAULT_EXCLUDES ,
427440 } ) ,
428441 } ) ,
@@ -479,7 +492,7 @@ describe('createVitestConfig', () => {
479492 const config = createVitestConfig ( options , overrides ) ;
480493 expectCoverageConfig ( config , {
481494 reporter : [ 'text' , 'lcov' , 'html' , 'json' ] ,
482- reportsDirectory : ` ${ MOCK_PROJECT_ROOT_STRING } /packages/ test-package/.coverage/ unit-tests` ,
495+ reportsDirectory : mockReportsDir ( ' test-package' , ' unit' ) ,
483496 exclude : [ ...DEFAULT_EXCLUDES , 'custom/**' ] ,
484497 thresholds : {
485498 global : {
@@ -536,7 +549,7 @@ describe('createVitestConfig', () => {
536549
537550 const testConfig = ( config as any ) . test ;
538551 expect ( testConfig . setupFiles ) . toEqual ( [
539- ` ${ MOCK_PROJECT_ROOT_STRING } / should-be-removed.ts` ,
552+ mockSetupFile ( ' should-be-removed.ts' ) ,
540553 ] ) ;
541554 expect ( testConfig . testTimeout ) . toBe ( TEST_TIMEOUTS . SHORT ) ;
542555 expect ( testConfig . pool ) . toBe ( 'forks' ) ;
@@ -579,7 +592,7 @@ describe('createVitestConfig', () => {
579592 expect ( ( config as any ) . test . testTimeout ) . toBe ( TEST_TIMEOUTS . SHORT ) ;
580593 expectCoverageConfig ( config , {
581594 reporter : [ 'text' , 'lcov' ] ,
582- reportsDirectory : ` ${ MOCK_PROJECT_ROOT_STRING } /packages/ test-package/.coverage/ unit-tests` ,
595+ reportsDirectory : mockReportsDir ( ' test-package' , ' unit' ) ,
583596 exclude : DEFAULT_EXCLUDES ,
584597 } ) ;
585598 } ) ;
@@ -603,7 +616,7 @@ describe('createVitestConfig', () => {
603616 expect ( ( config as any ) . test . testTimeout ) . toBe ( TEST_TIMEOUTS . SHORT ) ;
604617 expectCoverageConfig ( config , {
605618 reporter : [ 'text' , 'lcov' ] ,
606- reportsDirectory : ` ${ MOCK_PROJECT_ROOT_STRING } /packages/ test-package/.coverage/ unit-tests` ,
619+ reportsDirectory : mockReportsDir ( ' test-package' , ' unit' ) ,
607620 exclude : DEFAULT_EXCLUDES ,
608621 } ) ;
609622 } ) ;
@@ -632,9 +645,7 @@ describe('createVitestConfig', () => {
632645 expectedIncludes [ kind ] ,
633646 ) ;
634647 expect ( ( config as any ) . test . globalSetup ) . toStrictEqual (
635- kind === 'e2e'
636- ? undefined
637- : [ `${ MOCK_PROJECT_ROOT_STRING } /global-setup.ts` ] ,
648+ kind === 'e2e' ? undefined : [ mockGlobalSetup ( ) ] ,
638649 ) ;
639650 } ) ;
640651 } ) ;
@@ -681,15 +692,15 @@ describe('createVitestConfig', () => {
681692 } ,
682693 test : expect . objectContaining ( {
683694 setupFiles : [
684- ` ${ MOCK_PROJECT_ROOT_STRING } / setup1.ts` ,
685- ` ${ MOCK_PROJECT_ROOT_STRING } / setup2.ts` ,
695+ mockSetupFile ( ' setup1.ts' ) ,
696+ mockSetupFile ( ' setup2.ts' ) ,
686697 ] ,
687698 testTimeout : TEST_TIMEOUTS . LONG ,
688699 environment : 'jsdom' ,
689700 include : EXPECTED_INCLUDES . int ,
690701 coverage : expect . objectContaining ( {
691702 exclude : [ 'mocks/**' , '**/types.ts' , 'e2e/**' , 'dist/**' ] ,
692- reportsDirectory : ` ${ MOCK_PROJECT_ROOT_STRING } /packages/ test-package/.coverage/ int-tests` ,
703+ reportsDirectory : mockReportsDir ( ' test-package' , ' int' ) ,
693704 } ) ,
694705 } ) ,
695706 } ) ,
0 commit comments