Skip to content

Commit

Permalink
chore: refactor tests + clean up unused code in SearchSource (#12526)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon-leapyear authored Mar 2, 2022
1 parent 42566c1 commit ab74804
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 440 deletions.
16 changes: 8 additions & 8 deletions packages/jest-config/src/__tests__/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1577,12 +1577,19 @@ describe('testPathPattern', () => {
});

it(`joins multiple ${opt.name} if set`, async () => {
const argv = {testPathPattern: ['a/b', 'c/d']} as Config.Argv;
const argv = {[opt.property]: ['a/b', 'c/d']} as Config.Argv;
const {options} = await normalize(initialOptions, argv);

expect(options.testPathPattern).toBe('a/b|c/d');
});

it('coerces all patterns to strings', async () => {
const argv = {[opt.property]: [1]} as Config.Argv;
const {options} = await normalize(initialOptions, argv);

expect(options.testPathPattern).toBe('1');
});

describe('posix', () => {
it('should not escape the pattern', async () => {
const argv = {
Expand Down Expand Up @@ -1633,13 +1640,6 @@ describe('testPathPattern', () => {

expect(options.testPathPattern).toBe('a\\\\b|c\\\\d');
});

it('coerces all patterns to strings', async () => {
const argv = {[opt.property]: [1]} as Config.Argv;
const {options} = await normalize(initialOptions, argv);

expect(options.testPathPattern).toBe('1');
});
});
});
}
Expand Down
18 changes: 4 additions & 14 deletions packages/jest-core/src/SearchSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ export type SearchResult = {
total?: number;
};

export type TestSelectionConfig = {
input?: string;
findRelatedTests?: boolean;
onlyChanged?: boolean;
paths?: Array<string>;
shouldTreatInputAsPattern?: boolean;
testPathPattern?: string;
watch?: boolean;
};

const regexToMatcher = (testRegex: Config.ProjectConfig['testRegex']) => {
const regexes = testRegex.map(testRegex => new RegExp(testRegex));

Expand Down Expand Up @@ -121,7 +111,7 @@ export default class SearchSource {

private _filterTestPathsWithStats(
allPaths: Array<Test>,
testPathPattern?: string,
testPathPattern: string,
): SearchResult {
const data: {
stats: Stats;
Expand Down Expand Up @@ -163,7 +153,7 @@ export default class SearchSource {
return data;
}

private _getAllTestPaths(testPathPattern?: string): SearchResult {
private _getAllTestPaths(testPathPattern: string): SearchResult {
return this._filterTestPathsWithStats(
toTests(this._context, this._context.hasteFS.getAllFiles()),
testPathPattern,
Expand All @@ -174,7 +164,7 @@ export default class SearchSource {
return this._testPathCases.every(testCase => testCase.isMatch(path));
}

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

Expand Down Expand Up @@ -333,7 +323,7 @@ export default class SearchSource {

async getTestPaths(
globalConfig: Config.GlobalConfig,
changedFiles: ChangedFiles | undefined,
changedFiles?: ChangedFiles,
filter?: Filter,
): Promise<SearchResult> {
const searchResult = await this._getTestPaths(globalConfig, changedFiles);
Expand Down
22 changes: 0 additions & 22 deletions packages/jest-core/src/TestNamePatternPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,6 @@ export default class TestNamePatternPrompt extends PatternPrompt {
printRestoredPatternCaret(pattern, this._currentUsageRows, pipe);
}

_getMatchedTests(pattern: string): Array<string> {
let regex: RegExp;

try {
regex = new RegExp(pattern, 'i');
} catch {
return [];
}

const matchedTests: Array<string> = [];

this._cachedTestResults.forEach(({testResults}) =>
testResults.forEach(({title}) => {
if (regex.test(title)) {
matchedTests.push(title);
}
}),
);

return matchedTests;
}

updateCachedTestResults(testResults: Array<TestResult> = []): void {
this._cachedTestResults = testResults;
}
Expand Down
18 changes: 0 additions & 18 deletions packages/jest-core/src/TestPathPatternPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

import type {Test} from '@jest/test-result';
import type {Context} from 'jest-runtime';
import {
PatternPrompt,
Expand Down Expand Up @@ -41,23 +40,6 @@ export default class TestPathPatternPrompt extends PatternPrompt {
printRestoredPatternCaret(pattern, this._currentUsageRows, pipe);
}

_getMatchedTests(pattern: string): Array<Test> {
let regex;

try {
regex = new RegExp(pattern, 'i');
} catch {}

let tests: Array<Test> = [];
if (regex && this._searchSources) {
this._searchSources.forEach(({searchSource}) => {
tests = tests.concat(searchSource.findMatchingTests(pattern).tests);
});
}

return tests;
}

updateSearchSources(searchSources: SearchSources): void {
this._searchSources = searchSources;
}
Expand Down
Loading

0 comments on commit ab74804

Please sign in to comment.