Collection of utility functions for Jest testing framework
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Text formatting and path manipulation utilities for test output and file system operations. These functions provide cross-platform path handling and consistent text formatting for Jest's output systems.
Intelligently pluralizes words based on count, with customizable plural endings for proper grammatical output.
/**
* Pluralizes words based on count
* @param word - Base word to pluralize
* @param count - Count to determine pluralization
* @param ending - Plural ending to append (default: 's')
* @returns Formatted string with count and properly pluralized word
*/
function pluralize(word: string, count: number, ending = 's'): string;Usage Examples:
import { pluralize } from "jest-util";
// Basic pluralization
console.log(pluralize("test", 1)); // "1 test"
console.log(pluralize("test", 2)); // "2 tests"
console.log(pluralize("test", 0)); // "0 tests"
// Custom plural endings
console.log(pluralize("child", 1, "ren")); // "1 child"
console.log(pluralize("child", 2, "ren")); // "2 children"
console.log(pluralize("category", 1, "ies")); // "1 category"
console.log(pluralize("category", 2, "ies")); // "2 categoryies" (Note: removes 'y' and adds ending)
// Jest test output usage
function reportTestResults(passed: number, failed: number, skipped: number) {
console.log(`${pluralize("test", passed)} passed`);
console.log(`${pluralize("test", failed)} failed`);
console.log(`${pluralize("test", skipped)} skipped`);
}
// Examples:
// reportTestResults(1, 0, 0) → "1 test passed", "0 tests failed", "0 tests skipped"
// reportTestResults(5, 2, 1) → "5 tests passed", "2 tests failed", "1 test skipped"
// File operation reporting
const fileCount = 15;
console.log(`Processed ${pluralize("file", fileCount)}`); // "Processed 15 files"
const errorCount = 1;
console.log(`Found ${pluralize("error", errorCount)}`); // "Found 1 error"Formats time values with appropriate unit prefixes (nanoseconds, microseconds, milliseconds, seconds) for human-readable display.
/**
* Formats time with appropriate unit prefixes
* @param time - Time value to format
* @param prefixPower - Power of 10 prefix (default: -3 for milliseconds)
* @param padLeftLength - Left padding for time value (default: 0)
* @returns Formatted time string with appropriate unit
*/
function formatTime(time: number, prefixPower = -3, padLeftLength = 0): string;Usage Examples:
import { formatTime } from "jest-util";
// Default millisecond formatting
console.log(formatTime(1500)); // "1.5 s"
console.log(formatTime(250)); // "250 ms"
console.log(formatTime(45)); // "45 ms"
console.log(formatTime(0.5)); // "500 μs"
// Nanosecond precision (prefixPower: -9)
console.log(formatTime(1000000, -9)); // "1 ms"
console.log(formatTime(500000, -9)); // "500 μs"
console.log(formatTime(1500, -9)); // "1.5 μs"
console.log(formatTime(750, -9)); // "750 ns"
// Microsecond precision (prefixPower: -6)
console.log(formatTime(1000, -6)); // "1 ms"
console.log(formatTime(500, -6)); // "500 μs"
// With padding for aligned output
console.log(formatTime(5, -3, 6)); // " 5 ms"
console.log(formatTime(150, -3, 6)); // " 150 ms"
console.log(formatTime(1200, -3, 6)); // " 1.2 s"
// Jest test timing usage
function reportTestTiming(testName: string, duration: number) {
console.log(`${testName}: ${formatTime(duration)}`);
}
// Examples:
// reportTestTiming("API test", 1250) → "API test: 1.25 s"
// reportTestTiming("Unit test", 45) → "Unit test: 45 ms"
// reportTestTiming("Quick test", 0.8) → "Quick test: 800 μs"Converts Windows-style path separators to forward slashes for use in glob patterns, while preserving regex special characters.
/**
* Converts Windows path separators to forward slashes for glob patterns
* @param path - Path to convert
* @returns Path with forward slashes, preserving regex special characters
*/
function replacePathSepForGlob(path: string): string;Usage Examples:
import { replacePathSepForGlob } from "jest-util";
// Windows path conversion
console.log(replacePathSepForGlob("src\\components\\Button.tsx"));
// "src/components/Button.tsx"
console.log(replacePathSepForGlob("C:\\Projects\\MyApp\\test\\*.spec.js"));
// "C:/Projects/MyApp/test/*.spec.js"
// Unix paths remain unchanged
console.log(replacePathSepForGlob("src/components/Button.tsx"));
// "src/components/Button.tsx"
// Preserves regex special characters
console.log(replacePathSepForGlob("src\\test\\**\\*.{js,ts}"));
// "src/test/**/*.{js,ts}"
// Jest test pattern usage
const testPatterns = [
"src\\__tests__\\**\\*.test.js",
"tests\\integration\\*.spec.ts",
"**\\__mocks__\\**\\*"
];
const normalizedPatterns = testPatterns.map(replacePathSepForGlob);
// [
// "src/__tests__/**/*.test.js",
// "tests/integration/*.spec.ts",
// "**/__mocks__/**/*"
// ]Converts an array of glob patterns into an optimized matching function, with support for negated patterns and caching.
/**
* Converts glob patterns to optimized matching function
* @param globs - Array of glob patterns (supports negation with '!')
* @returns Matcher function that tests paths against patterns
*/
function globsToMatcher(globs: Array<string>): Matcher;
type Matcher = (str: string) => boolean;Usage Examples:
import { globsToMatcher } from "jest-util";
// Basic pattern matching
const matcher = globsToMatcher(["**/*.test.js", "**/*.spec.js"]);
console.log(matcher("src/utils.test.js")); // true
console.log(matcher("src/component.spec.js")); // true
console.log(matcher("src/utils.js")); // false
// Negated patterns (exclusions)
const testMatcher = globsToMatcher([
"**/*.test.js",
"**/*.spec.js",
"!**/node_modules/**",
"!**/build/**"
]);
console.log(testMatcher("src/utils.test.js")); // true
console.log(testMatcher("node_modules/lib/test.js")); // false (excluded)
console.log(testMatcher("build/test.spec.js")); // false (excluded)
// Complex Jest configuration
const jestMatcher = globsToMatcher([
"src/**/*.{js,ts}",
"test/**/*.{js,ts}",
"!**/*.d.ts",
"!**/node_modules/**",
"!**/coverage/**",
"!**/build/**"
]);
// Test file discovery
const candidateFiles = [
"src/components/Button.js",
"src/types.d.ts",
"test/Button.test.js",
"node_modules/react/index.js",
"coverage/report.js",
"build/main.js"
];
const matchingFiles = candidateFiles.filter(jestMatcher);
// ["src/components/Button.js", "test/Button.test.js"]
// Performance optimization - matcher is cached internally
const largeMatcher = globsToMatcher([
"**/*.{js,jsx,ts,tsx}",
"!**/node_modules/**",
"!**/dist/**",
"!**/*.min.js"
]);
// Multiple calls reuse compiled patterns for efficiency
const results = candidateFiles.map(file => largeMatcher(file));Pattern Syntax:
* - Matches any characters except path separators** - Matches any characters including path separators (recursive)? - Matches single character{a,b} - Matches either 'a' or 'b'!pattern - Negates the pattern (excludes matches)[abc] - Matches any character in bracketsPerformance Features:
type Matcher = (str: string) => boolean;