diff --git a/CHANGELOG.md b/CHANGELOG.md index 85c66a5d976f..f881d9a48448 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ ### Performance +- `[jest-core]` Cache micromatch in SearchSource globsToMatcher + ## 26.0.1 ### Fixes diff --git a/packages/jest-core/src/SearchSource.ts b/packages/jest-core/src/SearchSource.ts index 48c630f2adb4..1ce3021c8d88 100644 --- a/packages/jest-core/src/SearchSource.ts +++ b/packages/jest-core/src/SearchSource.ts @@ -37,8 +37,55 @@ export type TestSelectionConfig = { watch?: boolean; }; -const globsToMatcher = (globs: Array) => (path: Config.Path) => - micromatch([replacePathSepForGlob(path)], globs, {dot: true}).length > 0; +const globsMatchers = new Map< + string, + { + isMatch: (str: string) => boolean; + negated: boolean; + } +>(); + +const globsToMatcher = (globs: Array) => { + const matchers = globs.map(glob => { + if (!globsMatchers.has(glob)) { + const state = micromatch.scan(glob, {dot: true}); + const matcher = { + isMatch: micromatch.matcher(glob, {dot: true}), + negated: state.negated, + }; + globsMatchers.set(glob, matcher); + } + return globsMatchers.get(glob); + }); + + return (path: Config.Path) => { + const replacedPath = replacePathSepForGlob(path); + let omit = false; + let keep = false; + let negatives = 0; + + for (let i = 0; i < matchers.length; i++) { + const matcher = matchers[i]; + if (!matcher) continue; + const {isMatch, negated} = matcher; + if (negated) negatives++; + + const matched = isMatch(replacedPath); + + const match = negated ? !matched : matched; + if (!match) continue; + + if (negated) { + omit = true; + } else { + omit = false; + keep = true; + } + } + + return negatives === matchers.length ? !keep && !omit : keep && !omit; + }; +}; const regexToMatcher = (testRegex: Config.ProjectConfig['testRegex']) => ( path: Config.Path,