@@ -40,7 +40,7 @@ import { VitestSpecifications } from './specifications'
40
40
import { StateManager } from './state'
41
41
import { TestRun } from './test-run'
42
42
import { VitestWatcher } from './watcher'
43
- import { resolveBrowserWorkspace , resolveWorkspace } from './workspace/resolveWorkspace'
43
+ import { getDefaultTestProject , resolveBrowserWorkspace , resolveWorkspace } from './workspace/resolveWorkspace'
44
44
45
45
const WATCHER_DEBOUNCE = 100
46
46
@@ -90,14 +90,19 @@ export class Vitest {
90
90
/** @internal */ closingPromise ?: Promise < void >
91
91
/** @internal */ isCancelling = false
92
92
/** @internal */ coreWorkspaceProject : TestProject | undefined
93
- /** @internal */ resolvedProjects : TestProject [ ] = [ ]
93
+ /**
94
+ * @internal
95
+ * @deprecated
96
+ */
97
+ resolvedProjects : TestProject [ ] = [ ]
94
98
/** @internal */ _browserLastPort = defaultBrowserPort
95
99
/** @internal */ _browserSessions = new BrowserSessions ( )
96
100
/** @internal */ _options : UserConfig = { }
97
101
/** @internal */ reporters : Reporter [ ] = undefined !
98
102
/** @internal */ vitenode : ViteNodeServer = undefined !
99
103
/** @internal */ runner : ViteNodeRunner = undefined !
100
104
/** @internal */ _testRun : TestRun = undefined !
105
+ /** @internal */ _projectFilters : RegExp [ ] = [ ]
101
106
102
107
private isFirstRun = true
103
108
private restartsCount = 0
@@ -211,9 +216,11 @@ export class Vitest {
211
216
this . specifications . clearCache ( )
212
217
this . _onUserTestsRerun = [ ]
213
218
214
- const resolved = resolveConfig ( this . mode , options , server . config , this . logger )
215
-
219
+ this . _projectFilters = toArray ( options . project || [ ] ) . map ( project => wildcardPatternToRegExp ( project ) )
216
220
this . _vite = server
221
+
222
+ const resolved = resolveConfig ( this , options , server . config )
223
+
217
224
this . _config = resolved
218
225
this . _state = new StateManager ( )
219
226
this . _cache = new VitestCache ( this . version )
@@ -272,14 +279,8 @@ export class Vitest {
272
279
const projects = await this . resolveWorkspace ( cliOptions )
273
280
this . resolvedProjects = projects
274
281
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 ( '", "' ) } ".` )
283
284
}
284
285
if ( ! this . coreWorkspaceProject ) {
285
286
this . coreWorkspaceProject = TestProject . _createBasicProject ( this )
@@ -397,8 +398,15 @@ export class Vitest {
397
398
398
399
this . _workspaceConfigPath = workspaceConfigPath
399
400
401
+ // user doesn't have a workspace config, return default project
400
402
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 ] )
402
410
}
403
411
404
412
const workspaceModule = await this . import < {
@@ -858,15 +866,15 @@ export class Vitest {
858
866
/** @internal */
859
867
async changeProjectName ( pattern : string ) : Promise < void > {
860
868
if ( pattern === '' ) {
861
- delete this . configOverride . project
869
+ this . configOverride . project = undefined
870
+ this . _projectFilters = [ ]
862
871
}
863
872
else {
864
- this . configOverride . project = pattern
873
+ this . configOverride . project = [ pattern ]
874
+ this . _projectFilters = [ wildcardPatternToRegExp ( pattern ) ]
865
875
}
866
876
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 ( )
870
878
}
871
879
872
880
/** @internal */
@@ -1247,6 +1255,18 @@ export class Vitest {
1247
1255
onAfterSetServer ( fn : OnServerRestartHandler ) : void {
1248
1256
this . _onSetServer . push ( fn )
1249
1257
}
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
+ }
1250
1270
}
1251
1271
1252
1272
function assert ( condition : unknown , property : string , name : string = property ) : asserts condition {
0 commit comments