Skip to content

Commit

Permalink
Replace testPathPatternToRegExp with TestPathPatterns
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonchinn178 committed Sep 22, 2023
1 parent 225b6fe commit 993fc50
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 36 deletions.
19 changes: 9 additions & 10 deletions packages/jest-core/src/SearchSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {replaceRootDirInPath} from 'jest-config';
import {escapePathForRegex} from 'jest-regex-util';
import {DependencyResolver} from 'jest-resolve-dependencies';
import {buildSnapshotResolver} from 'jest-snapshot';
import {globsToMatcher, testPathPatternToRegExp} from 'jest-util';
import {TestPathPatterns, globsToMatcher} from 'jest-util';
import type {Filter, Stats, TestPathCases} from './types';

export type SearchResult = {
Expand Down Expand Up @@ -110,7 +110,7 @@ export default class SearchSource {

private _filterTestPathsWithStats(
allPaths: Array<Test>,
testPathPattern: string,
testPathPatterns: TestPathPatterns,
): SearchResult {
const data: {
stats: Stats;
Expand All @@ -128,10 +128,9 @@ export default class SearchSource {
};

const testCases = Array.from(this._testPathCases); // clone
if (testPathPattern) {
const regex = testPathPatternToRegExp(testPathPattern);
if (testPathPatterns.isSet()) {
testCases.push({
isMatch: (path: string) => regex.test(path),
isMatch: (path: string) => testPathPatterns.isMatch(path),
stat: 'testPathPattern',
});
data.stats.testPathPattern = 0;
Expand All @@ -152,19 +151,19 @@ export default class SearchSource {
return data;
}

private _getAllTestPaths(testPathPattern: string): SearchResult {
private _getAllTestPaths(testPathPatterns: TestPathPatterns): SearchResult {
return this._filterTestPathsWithStats(
toTests(this._context, this._context.hasteFS.getAllFiles()),
testPathPattern,
testPathPatterns,
);
}

isTestFilePath(path: string): boolean {
return this._testPathCases.every(testCase => testCase.isMatch(path));
}

findMatchingTests(testPathPattern: string): SearchResult {
return this._getAllTestPaths(testPathPattern);
findMatchingTests(testPathPatterns: TestPathPatterns): SearchResult {
return this._getAllTestPaths(testPathPatterns);
}

async findRelatedTests(
Expand Down Expand Up @@ -288,7 +287,7 @@ export default class SearchSource {
globalConfig.collectCoverage,
);
} else {
return this.findMatchingTests(globalConfig.testPathPattern);
return this.findMatchingTests(new TestPathPatterns(globalConfig));
}
}

Expand Down
6 changes: 5 additions & 1 deletion packages/jest-core/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {Config} from '@jest/types';
import type {IHasteMap as HasteMap} from 'jest-haste-map';
import {formatExecError} from 'jest-message-util';
import {
TestPathPatterns,
isInteractive,
preRunMessage,
requireOrImportModule,
Expand Down Expand Up @@ -229,9 +230,12 @@ export default async function watch(

const emitFileChange = () => {
if (hooks.isUsed('onFileChange')) {
const testPathPatterns = new TestPathPatterns([]);
const projects = searchSources.map(({context, searchSource}) => ({
config: context.config,
testPaths: searchSource.findMatchingTests('').tests.map(t => t.path),
testPaths: searchSource
.findMatchingTests(testPathPatterns)
.tests.map(t => t.path),
}));
hooks.getEmitter().onFileChange({projects});
}
Expand Down
11 changes: 5 additions & 6 deletions packages/jest-reporters/src/SummaryReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
TestContext,
} from '@jest/test-result';
import type {Config} from '@jest/types';
import {testPathPatternToRegExp} from 'jest-util';
import {TestPathPatterns} from 'jest-util';
import BaseReporter from './BaseReporter';
import getResultHeader from './getResultHeader';
import getSnapshotSummary from './getSnapshotSummary';
Expand Down Expand Up @@ -212,15 +212,14 @@ export default class SummaryReporter extends BaseReporter {
testContexts: Set<TestContext>,
globalConfig: Config.GlobalConfig,
) {
const testPathPatterns = new TestPathPatterns(globalConfig);

const getMatchingTestsInfo = () => {
const prefix = globalConfig.findRelatedTests
? ' related to files matching '
: ' matching ';

return (
chalk.dim(prefix) +
testPathPatternToRegExp(globalConfig.testPathPattern).toString()
);
return chalk.dim(prefix) + testPathPatterns.toPretty();
};

let testInfo = '';
Expand All @@ -229,7 +228,7 @@ export default class SummaryReporter extends BaseReporter {
testInfo = chalk.dim(' within paths');
} else if (globalConfig.onlyChanged) {
testInfo = chalk.dim(' related to changed files');
} else if (globalConfig.testPathPattern) {
} else if (testPathPatterns.isSet()) {
testInfo = getMatchingTestsInfo();
}

Expand Down
14 changes: 13 additions & 1 deletion packages/jest-util/src/TestPathPatterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@
* LICENSE file in the root directory of this source tree.
*/

import type {Config} from '@jest/types';
import {replacePathSepForRegex} from 'jest-regex-util';

type PatternsFullConfig = Pick<Config.GlobalConfig, 'testPathPatterns'>;

export default class TestPathPatterns {
readonly patterns: Array<string>;

private _regexString: string | null = null;

constructor(patterns: Array<string>) {
constructor(patterns: Array<string>);
constructor(config: PatternsFullConfig);
constructor(patternsOrConfig: Array<string> | PatternsFullConfig) {
let patterns;
if (Array.isArray(patternsOrConfig)) {
patterns = patternsOrConfig;
} else {
patterns = patternsOrConfig.testPathPatterns;
}

this.patterns = patterns;
}

Expand Down
1 change: 0 additions & 1 deletion packages/jest-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export {default as convertDescriptorToString} from './convertDescriptorToString'
export {specialChars};
export {default as replacePathSepForGlob} from './replacePathSepForGlob';
export {default as TestPathPatterns} from './TestPathPatterns';
export {default as testPathPatternToRegExp} from './testPathPatternToRegExp';
export {default as globsToMatcher} from './globsToMatcher';
export {preRunMessage};
export {default as pluralize} from './pluralize';
Expand Down
17 changes: 0 additions & 17 deletions packages/jest-util/src/testPathPatternToRegExp.ts

This file was deleted.

0 comments on commit 993fc50

Please sign in to comment.