Skip to content

Commit

Permalink
fix a couple more type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 27, 2019
1 parent 7b38a4a commit 733a683
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions packages/jest-core/src/SearchSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Config.Glob> | null) => {
Expand Down Expand Up @@ -105,27 +108,38 @@ export default class SearchSource {
testPathPattern?: string,
): SearchResult {
const data: {
stats: {[key in keyof TestPathCases]: number};
stats: {
[key in keyof (TestPathCases | TestPathCasesWithPathPattern)]: number
};
tests: Array<Test>;
total: number;
} = {
stats: {},
stats: {
roots: 0,
testMatch: 0,
testPathIgnorePatterns: 0,
testRegex: 0,
},
tests: [],
total: allPaths.length,
};

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<keyof TestPathCases>;
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;
Expand All @@ -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;
}

Expand Down

0 comments on commit 733a683

Please sign in to comment.