@@ -16,13 +16,7 @@ import {escapePathForRegex} from 'jest-regex-util';
1616import { replaceRootDirInPath } from 'jest-config' ;
1717import { buildSnapshotResolver } from 'jest-snapshot' ;
1818import { replacePathSepForGlob , testPathPatternToRegExp } from 'jest-util' ;
19- import {
20- Stats ,
21- TestPathCases ,
22- TestPathCasesWithPathPattern ,
23- TestPathCaseWithPathPatternStats ,
24- Filter ,
25- } from './types' ;
19+ import { TestPathCases , Filter , Stats } from './types' ;
2620
2721export type SearchResult = {
2822 noSCM ?: boolean ;
@@ -42,23 +36,11 @@ export type TestSelectionConfig = {
4236 watch ?: boolean ;
4337} ;
4438
45- const globsToMatcher = ( globs ?: Array < Config . Glob > | null ) => {
46- if ( globs == null || globs . length === 0 ) {
47- return ( ) => true ;
48- }
39+ const globsToMatcher = ( globs : Array < Config . Glob > ) => ( path : Config . Path ) =>
40+ micromatch . some ( replacePathSepForGlob ( path ) , globs , { dot : true } ) ;
4941
50- return ( path : Config . Path ) =>
51- micromatch . some ( replacePathSepForGlob ( path ) , globs , { dot : true } ) ;
52- } ;
53-
54- const regexToMatcher = ( testRegex : Array < string > ) => {
55- if ( ! testRegex . length ) {
56- return ( ) => true ;
57- }
58-
59- return ( path : Config . Path ) =>
60- testRegex . some ( testRegex => new RegExp ( testRegex ) . test ( path ) ) ;
61- } ;
42+ const regexToMatcher = ( testRegex : Array < string > ) => ( path : Config . Path ) =>
43+ testRegex . some ( testRegex => new RegExp ( testRegex ) . test ( path ) ) ;
6244
6345const toTests = ( context : Context , tests : Array < Config . Path > ) =>
6446 tests . map ( path => ( {
@@ -69,29 +51,43 @@ const toTests = (context: Context, tests: Array<Config.Path>) =>
6951
7052export default class SearchSource {
7153 private _context : Context ;
72- private _rootPattern : RegExp ;
73- private _testIgnorePattern : RegExp | null ;
74- private _testPathCases : TestPathCases ;
54+ private _testPathCases : TestPathCases = [ ] ;
7555
7656 constructor ( context : Context ) {
7757 const { config} = context ;
7858 this . _context = context ;
79- this . _rootPattern = new RegExp (
59+
60+ const rootPattern = new RegExp (
8061 config . roots . map ( dir => escapePathForRegex ( dir + path . sep ) ) . join ( '|' ) ,
8162 ) ;
63+ this . _testPathCases . push ( {
64+ isMatch : path => rootPattern . test ( path ) ,
65+ stat : 'roots' ,
66+ } ) ;
8267
83- const ignorePattern = config . testPathIgnorePatterns ;
84- this . _testIgnorePattern = ignorePattern . length
85- ? new RegExp ( ignorePattern . join ( '|' ) )
86- : null ;
87-
88- this . _testPathCases = {
89- roots : path => this . _rootPattern . test ( path ) ,
90- testMatch : globsToMatcher ( config . testMatch ) ,
91- testPathIgnorePatterns : path =>
92- ! this . _testIgnorePattern || ! this . _testIgnorePattern . test ( path ) ,
93- testRegex : regexToMatcher ( config . testRegex ) ,
94- } ;
68+ if ( config . testMatch . length ) {
69+ this . _testPathCases . push ( {
70+ isMatch : globsToMatcher ( config . testMatch ) ,
71+ stat : 'testMatch' ,
72+ } ) ;
73+ }
74+
75+ if ( config . testPathIgnorePatterns . length ) {
76+ const testIgnorePatternsRegex = new RegExp (
77+ config . testPathIgnorePatterns . join ( '|' ) ,
78+ ) ;
79+ this . _testPathCases . push ( {
80+ isMatch : path => ! testIgnorePatternsRegex . test ( path ) ,
81+ stat : 'testPathIgnorePatterns' ,
82+ } ) ;
83+ }
84+
85+ if ( config . testRegex . length ) {
86+ this . _testPathCases . push ( {
87+ isMatch : regexToMatcher ( config . testRegex ) ,
88+ stat : 'testRegex' ,
89+ } ) ;
90+ }
9591 }
9692
9793 private _filterTestPathsWithStats (
@@ -113,27 +109,27 @@ export default class SearchSource {
113109 total : allPaths . length ,
114110 } ;
115111
116- const testCases = Object . assign ( { } , this . _testPathCases ) ;
112+ const testCases = Array . from ( this . _testPathCases ) ; // clone
117113 if ( testPathPattern ) {
118114 const regex = testPathPatternToRegExp ( testPathPattern ) ;
119- ( testCases as TestPathCasesWithPathPattern ) . testPathPattern = (
120- path : Config . Path ,
121- ) => regex . test ( path ) ;
122- ( data . stats as TestPathCaseWithPathPatternStats ) . testPathPattern = 0 ;
115+ testCases . push ( {
116+ isMatch : ( path : Config . Path ) => regex . test ( path ) ,
117+ stat : 'testPathPattern' ,
118+ } ) ;
119+ data . stats . testPathPattern = 0 ;
123120 }
124121
125- const testCasesKeys = Object . keys ( testCases ) as Array < keyof Stats > ;
126- data . tests = allPaths . filter ( test =>
127- testCasesKeys . reduce < boolean > ( ( flag , key ) => {
128- const { stats } = data ;
129- if ( testCases [ key ] ( test . path ) ) {
130- stats [ key ] = stats [ key ] === undefined ? 1 : stats [ key ] + 1 ;
131- return flag && true ;
122+ data . tests = allPaths . filter ( test => {
123+ let filterResult = true ;
124+ for ( const { isMatch , stat } of testCases ) {
125+ if ( isMatch ( test . path ) ) {
126+ data . stats [ stat ] ! ++ ;
127+ } else {
128+ filterResult = false ;
132129 }
133- stats [ key ] = stats [ key ] || 0 ;
134- return false ;
135- } , true ) ,
136- ) ;
130+ }
131+ return filterResult ;
132+ } ) ;
137133
138134 return data ;
139135 }
@@ -146,9 +142,7 @@ export default class SearchSource {
146142 }
147143
148144 isTestFilePath ( path : Config . Path ) : boolean {
149- return ( Object . keys ( this . _testPathCases ) as Array <
150- keyof TestPathCases
151- > ) . every ( key => this . _testPathCases [ key ] ( path ) ) ;
145+ return this . _testPathCases . every ( testCase => testCase . isMatch ( path ) ) ;
152146 }
153147
154148 findMatchingTests ( testPathPattern ?: string ) : SearchResult {
0 commit comments