diff --git a/packages/jest-core/src/SearchSource.ts b/packages/jest-core/src/SearchSource.ts index 0c34b0bc38a1..fec3ab1fee60 100644 --- a/packages/jest-core/src/SearchSource.ts +++ b/packages/jest-core/src/SearchSource.ts @@ -43,9 +43,12 @@ type FilterResult = { type TestPathCases = { roots: (path: Config.Path) => boolean; testMatch: (path: Config.Path) => boolean; - testRegex: (path: Config.Path) => boolean; testPathIgnorePatterns: (path: Config.Path) => boolean; - testPathPattern?: (path: Config.Path) => boolean; + testRegex: (path: Config.Path) => boolean; +}; + +type TestPathCasesWithPathPattern = TestPathCases & { + testPathPattern: (path: Config.Path) => boolean; }; const globsToMatcher = (globs?: Array | null) => { @@ -105,11 +108,18 @@ export default class SearchSource { testPathPattern?: string, ): SearchResult { const data: { - stats: {[key in keyof TestPathCases]: number}; + stats: { + [key in keyof (TestPathCases | TestPathCasesWithPathPattern)]: number + }; tests: Array; total: number; } = { - stats: {}, + stats: { + roots: 0, + testMatch: 0, + testPathIgnorePatterns: 0, + testRegex: 0, + }, tests: [], total: allPaths.length, }; @@ -117,15 +127,19 @@ export default class SearchSource { const testCases = Object.assign({}, this._testPathCases); if (testPathPattern) { const regex = testPathPatternToRegExp(testPathPattern); - testCases.testPathPattern = (path: Config.Path) => regex.test(path); + (testCases as TestPathCasesWithPathPattern).testPathPattern = ( + path: Config.Path, + ) => regex.test(path); } - const testCasesKeys = Object.keys(testCases) as Array; + const testCasesKeys = Object.keys(testCases) as Array< + keyof (TestPathCases | TestPathCasesWithPathPattern) + >; data.tests = allPaths.filter(test => testCasesKeys.reduce((flag, key) => { if (testCases[key](test.path)) { if (data.stats[key] === undefined) { - data.stats[key] = 1; + data.stats[key] = 0; } ++data.stats[key]!; return flag && true; @@ -135,6 +149,13 @@ export default class SearchSource { }, true), ); + // TODO: Is this necessary? Done to keep the object the same as before the TS migration + testCasesKeys.forEach(key => { + if (data.stats[key] === 0) { + delete data.stats[key]; + } + }); + return data; }