1010import type { Context } from 'types/Context' ;
1111import type { Glob , GlobalConfig , Path } from 'types/Config' ;
1212import type { Test } from 'types/TestRunner' ;
13- import type { ChangedFilesPromise } from 'types/ChangedFiles' ;
13+ import type { ChangedFilesInfo } from 'types/ChangedFiles' ;
1414
1515import path from 'path' ;
1616import micromatch from 'micromatch' ;
@@ -24,7 +24,7 @@ import {replacePathSepForGlob} from 'jest-util';
2424type SearchResult = { |
2525 noSCM ? : boolean ,
2626 stats ?: { [ key : string ] : number } ,
27- collectCoverageFrom ?: Array < string > ,
27+ collectCoverageFrom ?: Set < string > ,
2828 tests : Array < Test > ,
2929 total ? : number ,
3030| } ;
@@ -158,29 +158,55 @@ export default class SearchSource {
158158 buildSnapshotResolver ( this . _context . config ) ,
159159 ) ;
160160
161- const tests = toTests (
162- this . _context ,
163- dependencyResolver . resolveInverse (
164- allPaths ,
165- this . isTestFilePath . bind ( this ) ,
166- {
167- skipNodeResolution : this . _context . config . skipNodeResolution ,
168- } ,
169- ) ,
170- ) ;
171- let collectCoverageFrom ;
172-
173- // If we are collecting coverage, also return collectCoverageFrom patterns
174- if ( collectCoverage ) {
175- collectCoverageFrom = Array . from ( allPaths ) . map ( filename => {
176- filename = replaceRootDirInPath ( this . _context . config . rootDir , filename ) ;
177- return path . isAbsolute ( filename )
178- ? path . relative ( this . _context . config . rootDir , filename )
179- : filename ;
180- } ) ;
161+ if ( ! collectCoverage ) {
162+ return {
163+ tests : toTests (
164+ this . _context ,
165+ dependencyResolver . resolveInverse (
166+ allPaths ,
167+ this . isTestFilePath . bind ( this ) ,
168+ { skipNodeResolution : this . _context . config . skipNodeResolution } ,
169+ ) ,
170+ ) ,
171+ } ;
181172 }
182173
183- return { collectCoverageFrom, tests} ;
174+ const testModulesMap = dependencyResolver . resolveInverseModuleMap (
175+ allPaths ,
176+ this . isTestFilePath . bind ( this ) ,
177+ { skipNodeResolution : this . _context . config . skipNodeResolution } ,
178+ ) ;
179+
180+ const allPathsAbsolute = Array . from ( allPaths ) . map ( p => path . resolve ( p ) ) ;
181+
182+ const collectCoverageFrom = new Set ( ) ;
183+
184+ testModulesMap . forEach ( testModule => {
185+ if ( ! testModule . dependencies ) {
186+ return ;
187+ }
188+
189+ testModule . dependencies
190+ . filter ( p => allPathsAbsolute . includes ( p ) )
191+ . map ( filename => {
192+ filename = replaceRootDirInPath (
193+ this . _context . config . rootDir ,
194+ filename ,
195+ ) ;
196+ return path . isAbsolute ( filename )
197+ ? path . relative ( this . _context . config . rootDir , filename )
198+ : filename ;
199+ } )
200+ . forEach ( filename => collectCoverageFrom . add ( filename ) ) ;
201+ } ) ;
202+
203+ return {
204+ collectCoverageFrom,
205+ tests : toTests (
206+ this . _context ,
207+ testModulesMap . map ( testModule => testModule . file ) ,
208+ ) ,
209+ } ;
184210 }
185211
186212 findTestsByPaths ( paths : Array < Path > ) : SearchResult {
@@ -207,11 +233,11 @@ export default class SearchSource {
207233 return { tests : [ ] } ;
208234 }
209235
210- async findTestRelatedToChangedFiles (
211- changedFilesPromise : ChangedFilesPromise ,
236+ findTestRelatedToChangedFiles (
237+ changedFilesInfo : ChangedFilesInfo ,
212238 collectCoverage : boolean ,
213239 ) {
214- const { repos, changedFiles} = await changedFilesPromise ;
240+ const { repos , changedFiles } = changedFilesInfo ;
215241 // no SCM (git/hg/...) is found in any of the roots.
216242 const noSCM = Object . keys ( repos ) . every ( scm => repos [ scm ] . size === 0 ) ;
217243 return noSCM
@@ -221,42 +247,38 @@ export default class SearchSource {
221247
222248 _getTestPaths (
223249 globalConfig : GlobalConfig ,
224- changedFilesPromise : ?ChangedFilesPromise ,
225- ) : Promise < SearchResult > {
250+ changedFiles : ?ChangedFilesInfo ,
251+ ) : SearchResult {
226252 const paths = globalConfig . nonFlagArgs ;
227253
228254 if ( globalConfig . onlyChanged ) {
229- if ( ! changedFilesPromise ) {
230- throw new Error ( 'This promise must be present when running with -o.' ) ;
255+ if ( ! changedFiles ) {
256+ throw new Error ( 'Changed files must be set when running with -o.' ) ;
231257 }
232258
233259 return this . findTestRelatedToChangedFiles (
234- changedFilesPromise ,
260+ changedFiles ,
235261 globalConfig . collectCoverage ,
236262 ) ;
237263 } else if ( globalConfig . runTestsByPath && paths && paths . length ) {
238- return Promise . resolve ( this . findTestsByPaths ( paths ) ) ;
264+ return this . findTestsByPaths ( paths ) ;
239265 } else if ( globalConfig . findRelatedTests && paths && paths . length ) {
240- return Promise . resolve (
241- this . findRelatedTestsFromPattern ( paths , globalConfig . collectCoverage ) ,
266+ return this . findRelatedTestsFromPattern (
267+ paths ,
268+ globalConfig . collectCoverage ,
242269 ) ;
243270 } else if ( globalConfig . testPathPattern != null ) {
244- return Promise . resolve (
245- this . findMatchingTests ( globalConfig . testPathPattern ) ,
246- ) ;
271+ return this . findMatchingTests ( globalConfig . testPathPattern ) ;
247272 } else {
248- return Promise . resolve ( { tests : [ ] } ) ;
273+ return { tests : [ ] } ;
249274 }
250275 }
251276
252277 async getTestPaths (
253278 globalConfig: GlobalConfig ,
254- changedFilesPromise : ?ChangedFilesPromise ,
279+ changedFiles : ?ChangedFilesInfo ,
255280 ) : Promise < SearchResult > {
256- const searchResult = await this . _getTestPaths (
257- globalConfig ,
258- changedFilesPromise ,
259- ) ;
281+ const searchResult = this . _getTestPaths ( globalConfig , changedFiles ) ;
260282
261283 const filterPath = globalConfig . filter ;
262284
0 commit comments