Helper library for Jest that implements the logic for parsing and matching test path patterns
npx @tessl/cli install tessl/npm-jest--pattern@30.0.0Jest Pattern is a helper library for the Jest testing framework that implements pattern parsing and matching logic for test path filtering. It provides cross-platform pattern matching capabilities that handle both absolute and relative file paths, with sophisticated regex-based matching and Windows/POSIX path separator normalization.
npm install @jest/patternimport { TestPathPatterns, TestPathPatternsExecutor, type TestPathPatternsExecutorOptions } from "@jest/pattern";For CommonJS:
const { TestPathPatterns, TestPathPatternsExecutor } = require("@jest/pattern");import { TestPathPatterns } from "@jest/pattern";
// Create pattern collection
const patterns = new TestPathPatterns(["**/*.test.js", "./src/**/*"]);
// Validate patterns
if (patterns.isValid()) {
// Create executor with root directory
const executor = patterns.toExecutor({ rootDir: "/project" });
// Test file paths against patterns
const matches = executor.isMatch("/project/src/utils.test.js"); // true
const noMatch = executor.isMatch("/project/docs/readme.md"); // false
}Jest Pattern is built around two primary components:
Manages collections of test path patterns with validation and conversion capabilities.
/**
* Manages collections of test path patterns for Jest test discovery
*/
class TestPathPatterns {
/** Array of pattern strings provided during construction */
readonly patterns: Array<string>;
/**
* Creates a new TestPathPatterns instance
* @param patterns - Array of pattern strings to manage
*/
constructor(patterns: Array<string>);
/**
* Check if any patterns are configured
* @returns true if patterns array is not empty
*/
isSet(): boolean;
/**
* Validate that all patterns form valid regular expressions
* @returns true if all patterns compile to valid regex
*/
isValid(): boolean;
/**
* Get human-readable representation of patterns
* @returns patterns joined with '|' separator
*/
toPretty(): string;
/**
* Create executor for pattern matching operations
* @param options - Configuration options including rootDir
* @returns TestPathPatternsExecutor instance
*/
toExecutor(options: TestPathPatternsExecutorOptions): TestPathPatternsExecutor;
/**
* Serialize patterns for Jest serializers
* @returns serialization object with patterns and type
*/
toJSON(): any;
}Executes pattern matching operations against file paths with cross-platform support.
/**
* Executes pattern matching operations with cross-platform path handling
*/
class TestPathPatternsExecutor {
/** Reference to the pattern collection */
readonly patterns: TestPathPatterns;
/**
* Creates a new TestPathPatternsExecutor
* @param patterns - TestPathPatterns instance to execute
* @param options - Configuration options for execution
*/
constructor(patterns: TestPathPatterns, options: TestPathPatternsExecutorOptions);
/**
* Check if any patterns are configured
* @returns true if patterns are set (delegates to patterns.isSet())
*/
isSet(): boolean;
/**
* Validate that all patterns form valid regular expressions
* @returns true if all patterns compile successfully
*/
isValid(): boolean;
/**
* Test if absolute path matches any configured pattern
* @param absPath - Absolute file path to test against patterns
* @returns true if path matches any pattern
* @throws Error if patterns form invalid regex
*/
isMatch(absPath: string): boolean;
/**
* Get human-readable representation of patterns
* @returns patterns joined with '|' separator (delegates to patterns.toPretty())
*/
toPretty(): string;
}Configuration interface for TestPathPatternsExecutor behavior.
/**
* Configuration options for TestPathPatternsExecutor
*/
interface TestPathPatternsExecutorOptions {
/** Root directory for resolving relative patterns */
rootDir: string;
}./src/**/*.test.js - matched against relative path from rootDir/project/tests/**/* - matched against absolute path\)/)./foo.test.js converts to ^foo.test.js regex.\foo.test.js converts to ^foo.test.js regexpath.relative(rootDir, absPath) for relative pattern matchingisValid() returns false for patterns that don't compile to valid regexisMatch() throws errors if patterns form invalid regex during executionpath module for reliable cross-platform path operationsimport { TestPathPatterns } from "@jest/pattern";
const patterns = new TestPathPatterns(["**/*.test.js", "!**/node_modules/**"]);
const executor = patterns.toExecutor({ rootDir: "/my-project" });
// Test various paths
console.log(executor.isMatch("/my-project/src/utils.test.js")); // true
console.log(executor.isMatch("/my-project/src/utils.js")); // false
console.log(executor.isMatch("/my-project/node_modules/lib/test.js")); // falseimport { TestPathPatterns } from "@jest/pattern";
const patterns = new TestPathPatterns(["[invalid-regex"]);
if (!patterns.isValid()) {
console.log("Invalid patterns detected");
// Handle invalid patterns appropriately
} else {
const executor = patterns.toExecutor({ rootDir: "/project" });
// Safe to use executor
}import { TestPathPatterns } from "@jest/pattern";
const patterns = new TestPathPatterns(["src/**/*.test.js", "tests/**/*"]);
console.log("Patterns:", patterns.toPretty()); // "src/**/*.test.js|tests/**/*"
const executor = patterns.toExecutor({ rootDir: "/project" });
console.log("Executor patterns:", executor.toPretty()); // Same output