Core functionality for Jest testing framework providing programmatic APIs for test discovery, scheduling, and CLI integration.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Core functionality for finding and filtering test files based on patterns, changed files, and dependencies. The SearchSource class provides comprehensive test discovery capabilities essential for watch mode, selective test running, and IDE integration.
The main class for test discovery operations, handling file pattern matching and dependency resolution.
/**
* Handles test discovery and filtering based on configuration patterns
*/
export default class SearchSource {
/**
* Creates a SearchSource instance with the given test context
* @param context - Test context containing configuration and file system access
*/
constructor(context: TestContext);
}Usage Example:
import { SearchSource } from "@jest/core";
import Runtime from "jest-runtime";
import { normalize } from "jest-config";
// Setup configuration and context
const { options: config } = await normalize({
rootDir: "./src",
testMatch: ["**/__tests__/**/*.test.js"],
}, {});
const context = await Runtime.createContext(config, {
maxWorkers: 1,
watchman: false,
});
// Create SearchSource instance
const searchSource = new SearchSource(context);Determines whether a given file path matches the configured test patterns.
/**
* Checks if a file path is considered a test file based on configuration
* @param path - File path to check
* @returns True if the path matches test patterns
*/
isTestFilePath(path: string): boolean;Usage Example:
const isTest = searchSource.isTestFilePath("/src/components/__tests__/Button.test.js");
// Returns: true
const isNotTest = searchSource.isTestFilePath("/src/components/Button.js");
// Returns: falseFinds all tests matching specific path patterns.
/**
* Finds tests matching the provided path patterns
* @param testPathPatternsExecutor - Pattern executor for filtering test paths
* @returns Search result containing matching tests and statistics
*/
findMatchingTests(testPathPatternsExecutor: TestPathPatternsExecutor): SearchResult;Usage Example:
import { TestPathPatterns } from "@jest/pattern";
const patterns = new TestPathPatterns(["Button"], "");
const executor = patterns.toExecutor();
const result = searchSource.findMatchingTests(executor);
console.log(`Found ${result.tests.length} tests`);
console.log("Statistics:", result.stats);Finds tests related to specific source files, useful for watch mode and coverage collection.
/**
* Finds tests related to a set of file paths using dependency analysis
* @param allPaths - Set of file paths to find related tests for
* @param collectCoverage - Whether to collect coverage information
* @returns Search result with related tests and optional coverage data
*/
async findRelatedTests(
allPaths: Set<string>,
collectCoverage: boolean
): Promise<SearchResult>;Usage Example:
// Find tests related to changed source files
const changedFiles = new Set([
"/src/components/Button.js",
"/src/utils/helpers.js"
]);
const result = await searchSource.findRelatedTests(changedFiles, true);
console.log(`Found ${result.tests.length} related tests`);
if (result.collectCoverageFrom) {
console.log("Coverage will be collected from:", result.collectCoverageFrom);
}Finds tests for specific file paths.
/**
* Returns tests for specific file paths
* @param paths - Array of file paths to check for tests
* @returns Search result containing tests from the specified paths
*/
findTestsByPaths(paths: Array<string>): SearchResult;Usage Example:
const testPaths = [
"/src/components/__tests__/Button.test.js",
"/src/utils/__tests__/helpers.test.js"
];
const result = searchSource.findTestsByPaths(testPaths);
console.log(`Found ${result.tests.length} tests at specified paths`);Finds related tests from path patterns with coverage collection support.
/**
* Finds related tests from path patterns
* @param paths - Array of file path patterns
* @param collectCoverage - Whether to collect coverage information
* @returns Search result with related tests
*/
async findRelatedTestsFromPattern(
paths: Array<string>,
collectCoverage: boolean
): Promise<SearchResult>;Finds tests related to changed files, optimized for version control integration.
/**
* Finds tests related to changed files from VCS information
* @param changedFilesInfo - Information about changed files from VCS
* @param collectCoverage - Whether to collect coverage information
* @returns Search result with tests related to changed files
*/
async findTestRelatedToChangedFiles(
changedFilesInfo: ChangedFiles,
collectCoverage: boolean
): Promise<SearchResult>;Gets test paths based on global and project configuration, supporting various Jest CLI options.
/**
* Gets test paths based on configuration and optional changed files
* @param globalConfig - Jest global configuration
* @param projectConfig - Project-specific configuration
* @param changedFiles - Optional changed files information
* @param filter - Optional filter function for test paths
* @returns Search result based on configuration
*/
async getTestPaths(
globalConfig: Config.GlobalConfig,
projectConfig: Config.ProjectConfig,
changedFiles?: ChangedFiles,
filter?: Filter
): Promise<SearchResult>;Finds source files related to changed test files.
/**
* Finds source files related to changed test files
* @param changedFilesInfo - Information about changed files
* @returns Array of related source file paths
*/
async findRelatedSourcesFromTestsInChangedFiles(
changedFilesInfo: ChangedFiles
): Promise<Array<string>>;Filters and resolves paths on Windows platform using case-insensitive matching.
/**
* Filters and resolves paths on Windows platform using case-insensitive matching
* @param paths - Array of file paths to filter and resolve
* @returns Array of resolved and filtered paths
*/
filterPathsWin32(paths: Array<string>): Array<string>;interface SearchResult {
/** Indicates if no source control management was found */
noSCM?: boolean;
/** Statistics about test path matching */
stats?: Stats;
/** Set of files to collect coverage from */
collectCoverageFrom?: Set<string>;
/** Array of discovered test files */
tests: Array<Test>;
/** Total number of tests found */
total?: number;
}
interface Stats {
/** Tests matching root directories */
roots: number;
/** Tests matching testMatch patterns */
testMatch: number;
/** Tests matching ignore patterns */
testPathIgnorePatterns: number;
/** Tests matching regex patterns */
testRegex: number;
/** Tests matching path patterns */
testPathPatterns?: number;
}
interface ChangedFiles {
/** Set of changed file paths */
changedFiles: Set<string>;
/** Repository information */
repos: {
git: Set<string>;
hg: Set<string>;
sl: Set<string>;
};
}