@@ -40,7 +40,7 @@ import { VitestSpecifications } from './specifications'
4040import { StateManager } from './state'
4141import { TestRun } from './test-run'
4242import { VitestWatcher } from './watcher'
43- import { resolveBrowserWorkspace , resolveWorkspace } from './workspace/resolveWorkspace'
43+ import { getDefaultTestProject , resolveBrowserWorkspace , resolveWorkspace } from './workspace/resolveWorkspace'
4444
4545const WATCHER_DEBOUNCE = 100
4646
@@ -90,14 +90,19 @@ export class Vitest {
9090 /** @internal */ closingPromise ?: Promise < void >
9191 /** @internal */ isCancelling = false
9292 /** @internal */ coreWorkspaceProject : TestProject | undefined
93- /** @internal */ resolvedProjects : TestProject [ ] = [ ]
93+ /**
94+ * @internal
95+ * @deprecated
96+ */
97+ resolvedProjects : TestProject [ ] = [ ]
9498 /** @internal */ _browserLastPort = defaultBrowserPort
9599 /** @internal */ _browserSessions = new BrowserSessions ( )
96100 /** @internal */ _options : UserConfig = { }
97101 /** @internal */ reporters : Reporter [ ] = undefined !
98102 /** @internal */ vitenode : ViteNodeServer = undefined !
99103 /** @internal */ runner : ViteNodeRunner = undefined !
100104 /** @internal */ _testRun : TestRun = undefined !
105+ /** @internal */ _projectFilters : RegExp [ ] = [ ]
101106
102107 private isFirstRun = true
103108 private restartsCount = 0
@@ -211,9 +216,11 @@ export class Vitest {
211216 this . specifications . clearCache ( )
212217 this . _onUserTestsRerun = [ ]
213218
214- const resolved = resolveConfig ( this . mode , options , server . config , this . logger )
215-
219+ this . _projectFilters = toArray ( options . project || [ ] ) . map ( project => wildcardPatternToRegExp ( project ) )
216220 this . _vite = server
221+
222+ const resolved = resolveConfig ( this , options , server . config )
223+
217224 this . _config = resolved
218225 this . _state = new StateManager ( )
219226 this . _cache = new VitestCache ( this . version )
@@ -272,14 +279,8 @@ export class Vitest {
272279 const projects = await this . resolveWorkspace ( cliOptions )
273280 this . resolvedProjects = projects
274281 this . projects = projects
275- const filters = toArray ( resolved . project ) . map ( s => wildcardPatternToRegExp ( s ) )
276- if ( filters . length > 0 ) {
277- this . projects = this . projects . filter ( p =>
278- filters . some ( pattern => pattern . test ( p . name ) ) ,
279- )
280- if ( ! this . projects . length ) {
281- throw new Error ( `No projects matched the filter "${ toArray ( resolved . project ) . join ( '", "' ) } ".` )
282- }
282+ if ( ! this . projects . length ) {
283+ throw new Error ( `No projects matched the filter "${ toArray ( resolved . project ) . join ( '", "' ) } ".` )
283284 }
284285 if ( ! this . coreWorkspaceProject ) {
285286 this . coreWorkspaceProject = TestProject . _createBasicProject ( this )
@@ -397,8 +398,15 @@ export class Vitest {
397398
398399 this . _workspaceConfigPath = workspaceConfigPath
399400
401+ // user doesn't have a workspace config, return default project
400402 if ( ! workspaceConfigPath ) {
401- return resolveBrowserWorkspace ( this , new Set ( ) , [ this . _ensureRootProject ( ) ] )
403+ // user can filter projects with --project flag, `getDefaultTestProject`
404+ // returns the project only if it matches the filter
405+ const project = getDefaultTestProject ( this )
406+ if ( ! project ) {
407+ return [ ]
408+ }
409+ return resolveBrowserWorkspace ( this , new Set ( ) , [ project ] )
402410 }
403411
404412 const workspaceModule = await this . import < {
@@ -858,15 +866,15 @@ export class Vitest {
858866 /** @internal */
859867 async changeProjectName ( pattern : string ) : Promise < void > {
860868 if ( pattern === '' ) {
861- delete this . configOverride . project
869+ this . configOverride . project = undefined
870+ this . _projectFilters = [ ]
862871 }
863872 else {
864- this . configOverride . project = pattern
873+ this . configOverride . project = [ pattern ]
874+ this . _projectFilters = [ wildcardPatternToRegExp ( pattern ) ]
865875 }
866876
867- this . projects = this . resolvedProjects . filter ( p => p . name === pattern )
868- const files = ( await this . globTestSpecifications ( ) ) . map ( spec => spec . moduleId )
869- await this . rerunFiles ( files , 'change project filter' , pattern === '' )
877+ await this . vite . restart ( )
870878 }
871879
872880 /** @internal */
@@ -1247,6 +1255,18 @@ export class Vitest {
12471255 onAfterSetServer ( fn : OnServerRestartHandler ) : void {
12481256 this . _onSetServer . push ( fn )
12491257 }
1258+
1259+ /**
1260+ * Check if the project with a given name should be included.
1261+ * @internal
1262+ */
1263+ _matchesProjectFilter ( name : string ) : boolean {
1264+ // no filters applied, any project can be included
1265+ if ( ! this . _projectFilters . length ) {
1266+ return true
1267+ }
1268+ return this . _projectFilters . some ( filter => filter . test ( name ) )
1269+ }
12501270}
12511271
12521272function assert ( condition : unknown , property : string , name : string = property ) : asserts condition {
0 commit comments