Jest's test runner responsible for orchestrating test execution, managing worker processes, and coordinating parallel test runs
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Worker setup and execution functions for parallel test processing across multiple worker processes. This system enables Jest to run tests concurrently while maintaining isolation and providing efficient resource management.
Initializes worker processes with serialized resolvers and module maps for efficient test execution.
/**
* Set up worker processes with necessary resolvers and module maps
* Called once per worker process during initialization
* @param setupData - Configuration data containing serializable resolvers
*/
export function setup(setupData: {
serializableResolvers: Array<SerializableResolver>;
}): void;Usage Example:
The setup function is internal to the jest-runner package and called automatically by the worker process management system. It is not part of the public API:
// This is called internally by TestRunner when spawning workers
// Users do not need to call this directlyExecutes individual tests within worker processes with proper context deserialization and error handling.
/**
* Execute a test within a worker process
* @param workerData - Test execution data including config and context
* @returns Promise resolving to test results
*/
export async function worker(workerData: {
config: Config.ProjectConfig;
globalConfig: Config.GlobalConfig;
path: string;
context: TestRunnerSerializedContext;
}): Promise<TestResult>;Usage Example:
The worker function is internal to the jest-runner package and called automatically by the worker process management system. It is not part of the public API:
// This is called internally by TestRunner via jest-worker
// Users do not need to call this directlyThe worker system is designed for efficient parallel test execution with proper isolation and resource management.
Worker Lifecycle:
jest-worker spawns worker processessetup() initializes resolvers and module mapsworker() processes individual test requestsProcess Isolation:
Efficient resolver setup and caching for module resolution within worker processes.
/**
* Serializable resolver data for worker processes
*/
interface SerializableResolver {
config: Config.ProjectConfig;
serializableModuleMap: SerializableModuleMap;
}Resolver Caching:
The setup function creates and caches resolvers for each project configuration:
// Internal resolver caching (not exported)
const resolvers = new Map<string, Resolver>();
// Resolver creation from serialized module maps
for (const { config, serializableModuleMap } of setupData.serializableResolvers) {
const moduleMap = HasteMap.getStatic(config).getModuleMapFromJSON(serializableModuleMap);
resolvers.set(config.id, Runtime.createResolver(config, moduleMap));
}Proper serialization and deserialization of test runner context for worker communication.
/**
* Serialized version of TestRunnerContext for worker processes
* Converts Sets to Arrays for JSON serialization
*/
interface TestRunnerSerializedContext {
changedFiles?: Array<string>;
sourcesRelatedToTestsInChangedFiles?: Array<string>;
}
/**
* Serialized setup data passed to worker processes during initialization
*/
interface SerializableResolver {
config: Config.ProjectConfig;
serializableModuleMap: SerializableModuleMap;
}Context Conversion: The worker function converts serialized arrays back to Sets:
// Convert serialized context back to Sets
const context: TestRunnerContext = {
...workerData.context,
changedFiles: workerData.context.changedFiles && new Set(workerData.context.changedFiles),
sourcesRelatedToTestsInChangedFiles: workerData.context.sourcesRelatedToTestsInChangedFiles &&
new Set(workerData.context.sourcesRelatedToTestsInChangedFiles)
};Robust error handling with proper serialization for cross-process communication.
Error Formatting:
interface SerializableError {
code?: string;
message: string;
stack?: string;
type: string;
}
/**
* Format errors for serialization across process boundaries
*/
const formatError = (error: string | ErrorWithCode): SerializableError => {
if (typeof error === 'string') {
const { message, stack } = separateMessageFromStack(error);
return { message, stack, type: 'Error' };
}
return {
code: error.code || undefined,
message: error.message,
stack: error.stack,
type: 'Error'
};
};Uncaught Exception Handling: Workers install global error handlers to prevent crashes:
process.on('uncaughtException', err => {
if (err.stack) {
console.error(err.stack);
} else {
console.error(err);
}
exit(1); // Uses exit-x for proper cleanup
});The worker system uses jest-worker's messaging system for communication between main and worker processes.
Message Flow:
Event Forwarding: Workers can send events back to the main process during test execution:
// Send events from worker to main process
const sendMessageToJest: TestFileEvent = (eventName, args) => {
messageParent([eventName, args]);
};
// Events are forwarded through the communication channel
await runTest(path, globalConfig, config, resolver, context, sendMessageToJest);Workers support various configuration options for optimal performance and resource management.
Worker Creation Options:
enableWorkerThreads: Use worker threads instead of child processesexposedMethods: Methods available for calling from main processforkOptions: Node.js child process options including serialization modeidleMemoryLimit: Memory limit for idle workersmaxRetries: Maximum retry attempts for failed operationsnumWorkers: Number of worker processes to spawnsetupArgs: Arguments passed to setup functionMemory Management: Workers can be configured with memory limits and monitoring:
// Worker memory limit configuration
idleMemoryLimit: typeof globalConfig.workerIdleMemoryLimit === 'number'
? globalConfig.workerIdleMemoryLimit
: undefinedProper cleanup procedures ensure workers shut down gracefully without resource leaks.
Graceful Shutdown:
Resource Management:
interface WorkerData {
config: Config.ProjectConfig;
globalConfig: Config.GlobalConfig;
path: string;
context: TestRunnerSerializedContext;
}
interface SerializableModuleMap {
duplicates: ReadonlyMap<string, DuplicatesSet>;
map: ReadonlyMap<string, ModuleMapItem>;
mocks: ReadonlyMap<string, string>;
rootDir: string;
}
interface ModuleMapItem {
[platform: string]: ModuleMapData;
}
interface ModuleMapData {
[key: string]: string;
}
interface DuplicatesSet {
[key: string]: {[key: string]: number};
}
interface ErrorWithCode extends Error {
code?: string;
}
type TestFileEvent = (
eventName: string,
args: Array<any>
) => void | Promise<void>;