diff --git a/packages/jest-worker/README.md b/packages/jest-worker/README.md index debdb3518e21..41bdb058e117 100644 --- a/packages/jest-worker/README.md +++ b/packages/jest-worker/README.md @@ -43,7 +43,7 @@ export function hello(param) { Node shipped with [`worker_threads`](https://nodejs.org/api/worker_threads.html), a "threading API" that uses `SharedArrayBuffers` to communicate between the main process and its child threads. This feature can significantly improve the communication time between parent and child processes in `jest-worker`. -To use `worker_threads` instead of default `child_process` you have to pass `enableWorkerThreads: true` when instantiating the worker. +To disable the default `worker_threads` and instead use `child_process` you have to pass `enableWorkerThreads: false` when instantiating the worker. ## API diff --git a/packages/jest-worker/src/WorkerPool.ts b/packages/jest-worker/src/WorkerPool.ts index 0bf2a72c3ed1..8c556732b8ca 100644 --- a/packages/jest-worker/src/WorkerPool.ts +++ b/packages/jest-worker/src/WorkerPool.ts @@ -15,6 +15,8 @@ import type { WorkerOptions, WorkerPoolInterface, } from './types'; +import ChildProcessWorker from './workers/ChildProcessWorker'; +import NodeThreadsWorker from './workers/NodeThreadsWorker'; class WorkerPool extends BaseWorkerPool implements WorkerPoolInterface { send( @@ -30,9 +32,9 @@ class WorkerPool extends BaseWorkerPool implements WorkerPoolInterface { createWorker(workerOptions: WorkerOptions): WorkerInterface { let Worker; if (this._options.enableWorkerThreads) { - Worker = require('./workers/NodeThreadsWorker').default; + Worker = NodeThreadsWorker; } else { - Worker = require('./workers/ChildProcessWorker').default; + Worker = ChildProcessWorker; } return new Worker(workerOptions); diff --git a/packages/jest-worker/src/__tests__/WorkerPool.test.js b/packages/jest-worker/src/__tests__/WorkerPool.test.js index 91283ef514b8..6aaba1739416 100644 --- a/packages/jest-worker/src/__tests__/WorkerPool.test.js +++ b/packages/jest-worker/src/__tests__/WorkerPool.test.js @@ -75,7 +75,6 @@ describe('WorkerPool', () => { it('should create a NodeThreadWorker and send to it', () => { jest.mock('worker_threads', () => 'Defined'); const workerPool = new WorkerPool('/path', { - enableWorkerThreads: true, forkOptions: {}, maxRetries: 1, numWorkers: 1, @@ -102,9 +101,10 @@ describe('WorkerPool', () => { ); }); - it('should avoid NodeThreadWorker if not passed enableWorkerThreads', () => { + it('should use ChildProcessWorker if passed enableWorkerThreads: false', () => { jest.mock('worker_threads', () => 'Defined'); const workerPool = new WorkerPool('/path', { + enableWorkerThreads: false, forkOptions: {}, maxRetries: 1, numWorkers: 1, diff --git a/packages/jest-worker/src/__tests__/thread-integration.test.js b/packages/jest-worker/src/__tests__/thread-integration.test.js index 537dcb2d1762..9992d7b5b2e4 100644 --- a/packages/jest-worker/src/__tests__/thread-integration.test.js +++ b/packages/jest-worker/src/__tests__/thread-integration.test.js @@ -65,7 +65,6 @@ describe('Jest Worker Process Integration', () => { it('calls a single method from the worker', async () => { const farm = new Farm('/tmp/baz.js', { - enableWorkerThreads: true, exposedMethods: ['foo', 'bar'], numWorkers: 4, }); @@ -79,7 +78,6 @@ describe('Jest Worker Process Integration', () => { it('distributes sequential calls across child processes', async () => { const farm = new Farm('/tmp/baz.js', { - enableWorkerThreads: true, exposedMethods: ['foo', 'bar'], numWorkers: 4, }); @@ -150,7 +148,6 @@ describe('Jest Worker Process Integration', () => { it('distributes concurrent calls across child processes', async () => { const farm = new Farm('/tmp/baz.js', { - enableWorkerThreads: true, exposedMethods: ['foo', 'bar'], numWorkers: 4, }); @@ -179,7 +176,6 @@ describe('Jest Worker Process Integration', () => { it('sticks parallel calls to children', async () => { const farm = new Farm('/tmp/baz.js', { computeWorkerKey: () => '1234567890abcdef', - enableWorkerThreads: true, exposedMethods: ['foo', 'bar'], numWorkers: 4, }); diff --git a/packages/jest-worker/src/index.ts b/packages/jest-worker/src/index.ts index caf909265a8d..65ba40bf0d7c 100644 --- a/packages/jest-worker/src/index.ts +++ b/packages/jest-worker/src/index.ts @@ -85,7 +85,7 @@ export class Worker { } const workerPoolOptions: WorkerPoolOptions = { - enableWorkerThreads: this._options.enableWorkerThreads ?? false, + enableWorkerThreads: this._options.enableWorkerThreads ?? true, forkOptions: this._options.forkOptions ?? {}, maxRetries: this._options.maxRetries ?? 3, numWorkers: this._options.numWorkers ?? Math.max(cpus().length - 1, 1),