@@ -50,6 +50,13 @@ export function setupBrowserRpc(globalServer: ParentBrowserProject) {
5050 )
5151 }
5252
53+ const method = searchParams . get ( 'method' ) as 'run' | 'collect'
54+ if ( method !== 'run' && method !== 'collect' ) {
55+ return error (
56+ new Error ( `[vitest] Method query in ${ request . url } is invalid. Method should be either "run" or "collect".` ) ,
57+ )
58+ }
59+
5360 if ( type === 'orchestrator' ) {
5461 const session = vitest . _browserSessions . getSession ( sessionId )
5562 // it's possible the session was already resolved by the preview provider
@@ -67,7 +74,7 @@ export function setupBrowserRpc(globalServer: ParentBrowserProject) {
6774 wss . handleUpgrade ( request , socket , head , ( ws ) => {
6875 wss . emit ( 'connection' , ws , request )
6976
70- const rpc = setupClient ( project , rpcId , ws )
77+ const rpc = setupClient ( project , rpcId , ws , method )
7178 const state = project . browser ! . state as BrowserServerState
7279 const clients = type === 'tester' ? state . testers : state . orchestrators
7380 clients . set ( rpcId , rpc )
@@ -96,7 +103,7 @@ export function setupBrowserRpc(globalServer: ParentBrowserProject) {
96103 }
97104 }
98105
99- function setupClient ( project : TestProject , rpcId : string , ws : WebSocket ) {
106+ function setupClient ( project : TestProject , rpcId : string , ws : WebSocket , method : 'run' | 'collect' ) {
100107 const mockResolver = new ServerMockResolver ( globalServer . vite , {
101108 moduleDirectories : project . config . server ?. deps ?. moduleDirectories ,
102109 } )
@@ -111,19 +118,39 @@ export function setupBrowserRpc(globalServer: ParentBrowserProject) {
111118 vitest . state . catchError ( error , type )
112119 } ,
113120 async onQueued ( file ) {
114- await vitest . _testRun . enqueued ( project , file )
121+ if ( method === 'collect' ) {
122+ vitest . state . collectFiles ( project , [ file ] )
123+ }
124+ else {
125+ await vitest . _testRun . enqueued ( project , file )
126+ }
115127 } ,
116128 async onCollected ( files ) {
117- await vitest . _testRun . collected ( project , files )
129+ if ( method === 'collect' ) {
130+ vitest . state . collectFiles ( project , files )
131+ }
132+ else {
133+ await vitest . _testRun . collected ( project , files )
134+ }
118135 } ,
119136 async onTaskUpdate ( packs , events ) {
120- await vitest . _testRun . updated ( packs , events )
137+ if ( method === 'collect' ) {
138+ vitest . state . updateTasks ( packs )
139+ }
140+ else {
141+ await vitest . _testRun . updated ( packs , events )
142+ }
121143 } ,
122144 onAfterSuiteRun ( meta ) {
123145 vitest . coverageProvider ?. onAfterSuiteRun ( meta )
124146 } ,
125- sendLog ( log ) {
126- return vitest . _testRun . log ( log )
147+ async sendLog ( log ) {
148+ if ( method === 'collect' ) {
149+ vitest . state . updateUserLog ( log )
150+ }
151+ else {
152+ await vitest . _testRun . log ( log )
153+ }
127154 } ,
128155 resolveSnapshotPath ( testPath ) {
129156 return vitest . snapshot . resolvePath < ResolveSnapshotPathHandlerContext > ( testPath , {
0 commit comments