11import  type  {  RuntimeRPC  }  from  '../../types/rpc' 
22import  type  {  TestProject  }  from  '../project' 
33import  type  {  ResolveSnapshotPathHandlerContext  }  from  '../types/config' 
4+ import  {  existsSync ,  mkdirSync  }  from  'node:fs' 
45import  {  fileURLToPath  }  from  'node:url' 
56import  {  cleanUrl  }  from  '@vitest/utils/helpers' 
67import  {  createFetchModuleFunction ,  handleRollupError  }  from  '../environments/fetchModule' 
@@ -13,9 +14,26 @@ interface MethodsOptions {
1314} 
1415
1516export  function  createMethodsRPC ( project : TestProject ,  options : MethodsOptions  =  { } ) : RuntimeRPC  { 
16-  const  ctx  =  project . vitest 
17+  const  vitest  =  project . vitest 
1718 const  cacheFs  =  options . cacheFs  ??  false 
18-  const  fetch  =  createFetchModuleFunction ( project . _resolver ,  cacheFs ,  project . tmpDir ) 
19+  project . vitest . state . metadata [ project . name ]  ??=  { 
20+  externalized : { } , 
21+  duration : { } , 
22+  tmps : { } , 
23+  } 
24+  if  ( project . config . dumpDir  &&  ! existsSync ( project . config . dumpDir ) )  { 
25+  mkdirSync ( project . config . dumpDir ,  {  recursive : true  } ) 
26+  } 
27+  project . vitest . state . metadata [ project . name ] . dumpDir  =  project . config . dumpDir 
28+  const  fetch  =  createFetchModuleFunction ( 
29+  project . _resolver , 
30+  cacheFs , 
31+  project . tmpDir , 
32+  { 
33+  dumpFolder : project . config . dumpDir , 
34+  readFromDump : project . config . server . debug ?. load  ??  process . env . VITEST_DEBUG_LOAD_DUMP  !=  null , 
35+  } , 
36+  ) 
1937 return  { 
2038 async  fetch ( 
2139 url , 
@@ -30,12 +48,20 @@ export function createMethodsRPC(project: TestProject, options: MethodsOptions =
3048
3149 const  start  =  performance . now ( ) 
3250
33-  try  { 
34-  return  await  fetch ( url ,  importer ,  environment ,  options ) 
35-  } 
36-  finally  { 
37-  project . vitest . state . transformTime  +=  ( performance . now ( )  -  start ) 
38-  } 
51+  return  await  fetch ( url ,  importer ,  environment ,  options ) . then ( ( result )  =>  { 
52+  const  duration  =  performance . now ( )  -  start 
53+  project . vitest . state . transformTime  +=  duration 
54+  const  metadata  =  project . vitest . state . metadata [ project . name ] 
55+  if  ( 'externalize'  in  result )  { 
56+  metadata . externalized [ url ]  =  result . externalize 
57+  } 
58+  if  ( 'tmp'  in  result )  { 
59+  metadata . tmps [ url ]  =  result . tmp 
60+  } 
61+  metadata . duration [ url ]  ??=  [ ] 
62+  metadata . duration [ url ] . push ( duration ) 
63+  return  result 
64+  } ) 
3965 } , 
4066 async  resolve ( id ,  importer ,  environmentName )  { 
4167 const  environment  =  project . vite . environments [ environmentName ] 
@@ -54,10 +80,10 @@ export function createMethodsRPC(project: TestProject, options: MethodsOptions =
5480 } , 
5581
5682 snapshotSaved ( snapshot )  { 
57-  ctx . snapshot . add ( snapshot ) 
83+  vitest . snapshot . add ( snapshot ) 
5884 } , 
5985 resolveSnapshotPath ( testPath : string )  { 
60-  return  ctx . snapshot . resolvePath < ResolveSnapshotPathHandlerContext > ( testPath ,  { 
86+  return  vitest . snapshot . resolvePath < ResolveSnapshotPathHandlerContext > ( testPath ,  { 
6187 config : project . serializedConfig , 
6288 } ) 
6389 } , 
@@ -73,50 +99,50 @@ export function createMethodsRPC(project: TestProject, options: MethodsOptions =
7399 } , 
74100 async  onQueued ( file )  { 
75101 if  ( options . collect )  { 
76-  ctx . state . collectFiles ( project ,  [ file ] ) 
102+  vitest . state . collectFiles ( project ,  [ file ] ) 
77103 } 
78104 else  { 
79-  await  ctx . _testRun . enqueued ( project ,  file ) 
105+  await  vitest . _testRun . enqueued ( project ,  file ) 
80106 } 
81107 } , 
82108 async  onCollected ( files )  { 
83109 if  ( options . collect )  { 
84-  ctx . state . collectFiles ( project ,  files ) 
110+  vitest . state . collectFiles ( project ,  files ) 
85111 } 
86112 else  { 
87-  await  ctx . _testRun . collected ( project ,  files ) 
113+  await  vitest . _testRun . collected ( project ,  files ) 
88114 } 
89115 } , 
90116 onAfterSuiteRun ( meta )  { 
91-  ctx . coverageProvider ?. onAfterSuiteRun ( meta ) 
117+  vitest . coverageProvider ?. onAfterSuiteRun ( meta ) 
92118 } , 
93119 async  onTaskAnnotate ( testId ,  annotation )  { 
94-  return  ctx . _testRun . annotate ( testId ,  annotation ) 
120+  return  vitest . _testRun . annotate ( testId ,  annotation ) 
95121 } , 
96122 async  onTaskUpdate ( packs ,  events )  { 
97123 if  ( options . collect )  { 
98-  ctx . state . updateTasks ( packs ) 
124+  vitest . state . updateTasks ( packs ) 
99125 } 
100126 else  { 
101-  await  ctx . _testRun . updated ( packs ,  events ) 
127+  await  vitest . _testRun . updated ( packs ,  events ) 
102128 } 
103129 } , 
104130 async  onUserConsoleLog ( log )  { 
105131 if  ( options . collect )  { 
106-  ctx . state . updateUserLog ( log ) 
132+  vitest . state . updateUserLog ( log ) 
107133 } 
108134 else  { 
109-  await  ctx . _testRun . log ( log ) 
135+  await  vitest . _testRun . log ( log ) 
110136 } 
111137 } , 
112138 onUnhandledError ( err ,  type )  { 
113-  ctx . state . catchError ( err ,  type ) 
139+  vitest . state . catchError ( err ,  type ) 
114140 } , 
115141 onCancel ( reason )  { 
116-  ctx . cancelCurrentRun ( reason ) 
142+  vitest . cancelCurrentRun ( reason ) 
117143 } , 
118144 getCountOfFailedTests ( )  { 
119-  return  ctx . state . getCountOfFailedTests ( ) 
145+  return  vitest . state . getCountOfFailedTests ( ) 
120146 } , 
121147 } 
122148} 
0 commit comments