88import type { Config } from '@jest/types' ;
99import { escapePathForRegex , replacePathSepForRegex } from 'jest-regex-util' ;
1010
11- type PatternsConfig = Pick < Config . GlobalConfig , 'rootDir' > ;
12- type PatternsFullConfig = PatternsConfig &
13- Pick < Config . GlobalConfig , 'testPathPatterns' > ;
11+ type PatternsConfig = {
12+ rootDir : string ;
13+ } ;
1414
1515export default class TestPathPatterns {
16- readonly patterns : Array < string > ;
17- private readonly rootDir : string ;
18-
1916 private _regexString : string | null = null ;
2017
21- constructor ( patterns : Array < string > , config : PatternsConfig ) ;
22- constructor ( config : PatternsFullConfig ) ;
2318 constructor (
24- patternsOrConfig : Array < string > | PatternsFullConfig ,
25- configArg ?: PatternsConfig ,
26- ) {
27- let patterns , config ;
28- if ( Array . isArray ( patternsOrConfig ) ) {
29- patterns = patternsOrConfig ;
30- config = configArg ! ;
31- } else {
32- patterns = patternsOrConfig . testPathPatterns ;
33- config = patternsOrConfig ;
34- }
19+ readonly patterns : Array < string > ,
20+ private readonly config : PatternsConfig ,
21+ ) { }
3522
36- this . patterns = patterns ;
37- this . rootDir = config . rootDir . replace ( / \/ * $ / , '/' ) ;
23+ static fromGlobalConfig ( globalConfig : Config . GlobalConfig ) : TestPathPatterns {
24+ return new TestPathPatterns ( globalConfig . testPathPatterns , globalConfig ) ;
3825 }
3926
4027 private get regexString ( ) : string {
4128 if ( this . _regexString !== null ) {
4229 return this . _regexString ;
4330 }
44- const rootDirRegex = escapePathForRegex ( this . rootDir ) ;
31+
32+ const rootDir = this . config . rootDir . replace ( / \/ * $ / , '/' ) ;
33+ const rootDirRegex = escapePathForRegex ( rootDir ) ;
34+
4535 const regexString = this . patterns
4636 . map ( p => {
4737 // absolute paths passed on command line should stay same
@@ -59,6 +49,7 @@ export default class TestPathPatterns {
5949 } )
6050 . map ( replacePathSepForRegex )
6151 . join ( '|' ) ;
52+
6253 this . _regexString = regexString ;
6354 return regexString ;
6455 }
0 commit comments