Skip to content

Commit

Permalink
Don't overload TestPathPatterns constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonchinn178 committed Sep 25, 2023
1 parent e228478 commit de9edca
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 29 deletions.
4 changes: 3 additions & 1 deletion packages/jest-core/src/SearchSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ export default class SearchSource {
globalConfig.collectCoverage,
);
} else {
return this.findMatchingTests(new TestPathPatterns(globalConfig));
return this.findMatchingTests(
TestPathPatterns.fromGlobalConfig(globalConfig),
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/getNoTestFound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function getNoTestFound(
.map(p => `"${p}"`)
.join(', ')}`;
} else {
const testPathPatterns = new TestPathPatterns(globalConfig);
const testPathPatterns = TestPathPatterns.fromGlobalConfig(globalConfig);
dataMessage = `Pattern: ${chalk.yellow(
testPathPatterns.toPretty(),
)} - 0 matches`;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/getNoTestFoundVerbose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default function getNoTestFoundVerbose(
.map(p => `"${p}"`)
.join(', ')}`;
} else {
const testPathPatterns = new TestPathPatterns(globalConfig);
const testPathPatterns = TestPathPatterns.fromGlobalConfig(globalConfig);
dataMessage = `Pattern: ${chalk.yellow(
testPathPatterns.toPretty(),
)} - 0 matches`;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/lib/activeFiltersMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {TestPathPatterns, isNonNullable} from 'jest-util';

const activeFilters = (globalConfig: Config.GlobalConfig): string => {
const {testNamePattern} = globalConfig;
const testPathPatterns = new TestPathPatterns(globalConfig);
const testPathPatterns = TestPathPatterns.fromGlobalConfig(globalConfig);
if (testNamePattern || testPathPatterns.isSet()) {
const filters = [
testPathPatterns.isSet()
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/lib/updateGlobalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function updateGlobalConfig(
newConfig.onlyChanged =
!newConfig.watchAll &&
!newConfig.testNamePattern &&
!new TestPathPatterns(newConfig).isSet();
!TestPathPatterns.fromGlobalConfig(newConfig).isSet();

if (typeof options.bail === 'boolean') {
newConfig.bail = options.bail ? 1 : 0;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ const usage = (
watchPlugins: Array<WatchPlugin>,
delimiter = '\n',
) => {
const testPathPatterns = new TestPathPatterns(globalConfig);
const testPathPatterns = TestPathPatterns.fromGlobalConfig(globalConfig);
const messages = [
activeFilters(globalConfig),

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-reporters/src/SummaryReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export default class SummaryReporter extends BaseReporter {
testContexts: Set<TestContext>,
globalConfig: Config.GlobalConfig,
) {
const testPathPatterns = new TestPathPatterns(globalConfig);
const testPathPatterns = TestPathPatterns.fromGlobalConfig(globalConfig);

const getMatchingTestsInfo = () => {
const prefix = globalConfig.findRelatedTests
Expand Down
35 changes: 13 additions & 22 deletions packages/jest-util/src/TestPathPatterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,30 @@
import type {Config} from '@jest/types';
import {escapePathForRegex, replacePathSepForRegex} from 'jest-regex-util';

type PatternsConfig = Pick<Config.GlobalConfig, 'rootDir'>;
type PatternsFullConfig = PatternsConfig &
Pick<Config.GlobalConfig, 'testPathPatterns'>;
type PatternsConfig = {
rootDir: string;
};

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

private _regexString: string | null = null;

constructor(patterns: Array<string>, config: PatternsConfig);
constructor(config: PatternsFullConfig);
constructor(
patternsOrConfig: Array<string> | PatternsFullConfig,
configArg?: PatternsConfig,
) {
let patterns, config;
if (Array.isArray(patternsOrConfig)) {
patterns = patternsOrConfig;
config = configArg!;
} else {
patterns = patternsOrConfig.testPathPatterns;
config = patternsOrConfig;
}
readonly patterns: Array<string>,
private readonly config: PatternsConfig,
) {}

this.patterns = patterns;
this.rootDir = config.rootDir.replace(/\/*$/, '/');
static fromGlobalConfig(globalConfig: Config.GlobalConfig): TestPathPatterns {
return new TestPathPatterns(globalConfig.testPathPatterns, globalConfig);
}

private get regexString(): string {
if (this._regexString !== null) {
return this._regexString;
}
const rootDirRegex = escapePathForRegex(this.rootDir);

const rootDir = this.config.rootDir.replace(/\/*$/, '/');
const rootDirRegex = escapePathForRegex(rootDir);

const regexString = this.patterns
.map(p => {
// absolute paths passed on command line should stay same
Expand All @@ -59,6 +49,7 @@ export default class TestPathPatterns {
})
.map(replacePathSepForRegex)
.join('|');

this._regexString = regexString;
return regexString;
}
Expand Down

0 comments on commit de9edca

Please sign in to comment.