jest-worker

  • Version 28.1.3
  • Published
  • 69.1 kB
  • 3 dependencies
  • MIT license

Install

npm i jest-worker
yarn add jest-worker
pnpm add jest-worker

Overview

Module for executing heavy tasks under forked processes in parallel, by providing a `Promise` based interface, minimum overhead, and bound workers.

Index

Variables

variable CHILD_MESSAGE_CALL

const CHILD_MESSAGE_CALL: number;

    variable CHILD_MESSAGE_END

    const CHILD_MESSAGE_END: number;

      variable CHILD_MESSAGE_INITIALIZE

      const CHILD_MESSAGE_INITIALIZE: number;

        Functions

        function messageParent

        messageParent: (message: unknown, parentProcess?: NodeJS.Process) => void;

          Classes

          class FifoQueue

          class FifoQueue implements TaskQueue {}
          • First-in, First-out task queue that manages a dedicated pool for each worker as well as a shared queue. The FIFO ordering is guaranteed across the worker specific and shared queue.

          method dequeue

          dequeue: (workerId: number) => QueueChildMessage | null;

            method enqueue

            enqueue: (task: QueueChildMessage, workerId?: number) => void;

              class PriorityQueue

              class PriorityQueue implements TaskQueue {}
              • Priority queue that processes tasks in natural ordering (lower priority first) according to the priority computed by the function passed in the constructor.

                FIFO ordering isn't guaranteed for tasks with the same priority.

                Worker specific tasks with the same priority as a non-worker specific task are always processed first.

              constructor

              constructor(_computePriority: ComputeTaskPriorityCallback);

                method dequeue

                dequeue: (workerId: number) => QueueChildMessage | null;

                  method enqueue

                  enqueue: (task: QueueChildMessage, workerId?: number) => void;

                    class Worker

                    class Worker_2 {}
                    • The Jest farm (publicly called "Worker") is a class that allows you to queue methods across multiple child processes, in order to parallelize work. This is done by providing an absolute path to a module that will be loaded on each of the child processes, and bridged to the main process.

                      Bridged methods are specified by using the "exposedMethods" property of the "options" object. This is an array of strings, where each of them corresponds to the exported name in the loaded module.

                      You can also control the amount of workers by using the "numWorkers" property of the "options" object, and the settings passed to fork the process through the "forkOptions" property. The amount of workers defaults to the amount of CPUS minus one.

                      Queueing calls can be done in two ways: - Standard method: calls will be redirected to the first available worker, so they will get executed as soon as they can.

                      - Sticky method: if a "computeWorkerKey" method is provided within the config, the resulting string of this method will be used as a key. Every time this key is returned, it is guaranteed that your job will be processed by the same worker. This is specially useful if your workers are caching results.

                    constructor

                    constructor(workerPath: string, options?: WorkerFarmOptions);

                      method end

                      end: () => Promise<PoolExitResult>;

                        method getStderr

                        getStderr: () => NodeJS.ReadableStream;

                          method getStdout

                          getStdout: () => NodeJS.ReadableStream;

                            Interfaces

                            interface PromiseWithCustomMessage

                            interface PromiseWithCustomMessage<T> extends Promise<T> {}

                              property UNSTABLE_onCustomMessage

                              UNSTABLE_onCustomMessage?: (listener: OnCustomMessage) => () => void;

                                interface TaskQueue

                                interface TaskQueue {}

                                  method dequeue

                                  dequeue: (workerId: number) => QueueChildMessage | null;
                                  • Dequeues the next item from the queue for the specified worker

                                    Parameter workerId

                                    the id of the worker for which the next task should be retrieved

                                  method enqueue

                                  enqueue: (task: QueueChildMessage, workerId?: number) => void;
                                  • Enqueues the task in the queue for the specified worker or adds it to the queue shared by all workers

                                    Parameter task

                                    the task to queue

                                    Parameter workerId

                                    the id of the worker that should process this task or undefined if there's no preference.

                                  interface WorkerPoolInterface

                                  interface WorkerPoolInterface {}

                                    property send

                                    send: WorkerCallback;

                                      method createWorker

                                      createWorker: (options: WorkerOptions_2) => WorkerInterface;

                                        method end

                                        end: () => Promise<PoolExitResult>;

                                          method getStderr

                                          getStderr: () => NodeJS.ReadableStream;

                                            method getStdout

                                            getStdout: () => NodeJS.ReadableStream;

                                              method getWorkers

                                              getWorkers: () => Array<WorkerInterface>;

                                                Type Aliases

                                                type JestWorkerFarm

                                                type JestWorkerFarm<T extends Record<string, unknown>> = Worker_2 & WorkerModule<T>;

                                                  type WorkerFarmOptions

                                                  type WorkerFarmOptions = {
                                                  computeWorkerKey?: (method: string, ...args: Array<unknown>) => string | null;
                                                  enableWorkerThreads?: boolean;
                                                  exposedMethods?: ReadonlyArray<string>;
                                                  forkOptions?: ForkOptions;
                                                  maxRetries?: number;
                                                  numWorkers?: number;
                                                  resourceLimits?: ResourceLimits;
                                                  setupArgs?: Array<unknown>;
                                                  taskQueue?: TaskQueue;
                                                  WorkerPool?: new (
                                                  workerPath: string,
                                                  options?: WorkerPoolOptions
                                                  ) => WorkerPoolInterface;
                                                  workerSchedulingPolicy?: WorkerSchedulingPolicy;
                                                  };

                                                    type WorkerPoolOptions

                                                    type WorkerPoolOptions = {
                                                    setupArgs: Array<unknown>;
                                                    forkOptions: ForkOptions;
                                                    resourceLimits: ResourceLimits;
                                                    maxRetries: number;
                                                    numWorkers: number;
                                                    enableWorkerThreads: boolean;
                                                    };

                                                      Package Files (1)

                                                      Dependencies (3)

                                                      Dev Dependencies (7)

                                                      Peer Dependencies (0)

                                                      No peer dependencies.

                                                      Badge

                                                      To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                      You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/jest-worker.

                                                      • Markdown
                                                        [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/jest-worker)
                                                      • HTML
                                                        <a href="https://www.jsdocs.io/package/jest-worker"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>