Skip to content

Commit 9cfcd81

Browse files
authored
make worker_threads opt-in, not opt-out (#7693)
1 parent 6de22dd commit 9cfcd81

10 files changed

Lines changed: 14 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
- `[jest-runner]` Instantiate the test environment class with the current `testPath` ([#7442](https://github.com/facebook/jest/pull/7442))
5353
- `[jest-config]` Always resolve jest-environment-jsdom from jest-config ([#7476](https://github.com/facebook/jest/pull/7476))
5454
- `[expect]` Improve report when assertion fails, part 6 ([#7621](https://github.com/facebook/jest/pull/7621))
55-
- `[jest-worker]` Add `disableWorkerThreads` option to explicitly opt out of `worker_threads` even if available ([#7681](https://github.com/facebook/jest/pull/7681))
55+
- `[jest-worker]` Add `enableWorkerThreads` option to explicitly opt-in to `worker_threads` if available ([#7681](https://github.com/facebook/jest/pull/7681))
5656

5757
### Fixes
5858

packages/jest-cli/src/reporters/coverage_reporter.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ export default class CoverageReporter extends BaseReporter {
155155
} else {
156156
// $FlowFixMe: assignment of a worker with custom properties.
157157
worker = new Worker(require.resolve('./coverage_worker'), {
158-
disableWorkerThreads: true,
159158
exposedMethods: ['worker'],
160159
maxRetries: 2,
161160
numWorkers: this._globalConfig.maxWorkers,

packages/jest-haste-map/src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,6 @@ class HasteMap extends EventEmitter {
672672
} else {
673673
// $FlowFixMe: assignment of a worker with custom properties.
674674
this._worker = (new Worker(require.resolve('./worker'), {
675-
disableWorkerThreads: true,
676675
exposedMethods: ['getSha1', 'worker'],
677676
maxRetries: 3,
678677
numWorkers: this._options.maxWorkers,

packages/jest-runner/src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ class TestRunner {
9696
) {
9797
// $FlowFixMe: class object is augmented with worker when instantiating.
9898
const worker: WorkerInterface = new Worker(TEST_WORKER_PATH, {
99-
disableWorkerThreads: true,
10099
exposedMethods: ['worker'],
101100
forkOptions: {stdio: 'pipe'},
102101
maxRetries: 3,

packages/jest-worker/README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ export function hello(param) {
4343

4444
Node 10 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 experimental Node feature can significantly improve the communication time between parent and child processes in `jest-worker`.
4545

46-
We will use worker threads where available. To enable in Node 10+, run the Node process with the `--experimental-worker` flag.
47-
48-
You can explicitly opt-out of this by passing `disableWorkerThreads: true`.
46+
Since `worker_threads` are considered experimental in Node, you have to opt-in to this behavior by passing `enableWorkerThreads: true` when instantiating the worker. While the feature was unflagged in Node 11.7.0, you'll need to run the Node process with the `--experimental-worker` flag for Node 10.
4947

5048
## API
5149

@@ -91,9 +89,9 @@ Provide a custom worker pool to be used for spawning child processes. By default
9189

9290
The arguments that will be passed to the `setup` method during initialization.
9391

94-
#### `disableWorkerThreads: boolean` (optional)
92+
#### `enableWorkerThreads: boolean` (optional)
9593

96-
`jest-worker` will automatically detect if `worker_threads` are available and use them. However, running under threads comes with [some caveats](https://nodejs.org/api/worker_threads.html#worker_threads_class_worker), and is still experimental, so you can `opt-out` of this and use `disableWorkerThreads: true`.
94+
`jest-worker` will automatically detect if `worker_threads` are available, but will not use them unless passed `enableWorkerThreads: true`.
9795

9896
## Worker
9997

packages/jest-worker/src/WorkerPool.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class WorkerPool extends BaseWorkerPool implements WorkerPoolInterface {
4242

4343
createWorker(workerOptions: WorkerOptions): WorkerInterface {
4444
let Worker;
45-
if (!this._options.disableWorkerThreads && canUseWorkerThreads()) {
45+
if (this._options.enableWorkerThreads && canUseWorkerThreads()) {
4646
Worker = require('./workers/NodeThreadsWorker').default;
4747
} else {
4848
Worker = require('./workers/ChildProcessWorker').default;

packages/jest-worker/src/__tests__/WorkerPool.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ describe('WorkerPool', () => {
7777
it('should create a NodeThreadWorker and send to it', () => {
7878
jest.mock('worker_threads', () => 'Defined');
7979
const workerPool = new WorkerPool('/path', {
80+
enableWorkerThreads: true,
8081
forkOptions: {},
8182
maxRetries: 1,
8283
numWorkers: 1,
@@ -102,10 +103,9 @@ describe('WorkerPool', () => {
102103
);
103104
});
104105

105-
it('should avoid NodeThreadWorker if passed disableWorkerThreads', () => {
106+
it('should avoid NodeThreadWorker if not passed enableWorkerThreads', () => {
106107
jest.mock('worker_threads', () => 'Defined');
107108
const workerPool = new WorkerPool('/path', {
108-
disableWorkerThreads: true,
109109
forkOptions: {},
110110
maxRetries: 1,
111111
numWorkers: 1,

packages/jest-worker/src/__tests__/thread-integration.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ describe('Jest Worker Process Integration', () => {
6666

6767
it('calls a single method from the worker', async () => {
6868
const farm = new Farm('/tmp/baz.js', {
69+
enableWorkerThreads: true,
6970
exposedMethods: ['foo', 'bar'],
7071
numWorkers: 4,
7172
});
@@ -79,6 +80,7 @@ describe('Jest Worker Process Integration', () => {
7980

8081
it('distributes sequential calls across child processes', async () => {
8182
const farm = new Farm('/tmp/baz.js', {
83+
enableWorkerThreads: true,
8284
exposedMethods: ['foo', 'bar'],
8385
numWorkers: 4,
8486
});
@@ -100,6 +102,7 @@ describe('Jest Worker Process Integration', () => {
100102

101103
it('distributes concurrent calls across child processes', async () => {
102104
const farm = new Farm('/tmp/baz.js', {
105+
enableWorkerThreads: true,
103106
exposedMethods: ['foo', 'bar'],
104107
numWorkers: 4,
105108
});
@@ -128,6 +131,7 @@ describe('Jest Worker Process Integration', () => {
128131
it('sticks parallel calls to children', async () => {
129132
const farm = new Farm('/tmp/baz.js', {
130133
computeWorkerKey: () => '1234567890abcdef',
134+
enableWorkerThreads: true,
131135
exposedMethods: ['foo', 'bar'],
132136
numWorkers: 4,
133137
});

packages/jest-worker/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default class JestWorker {
7777
this._options = {...options};
7878

7979
const workerPoolOptions: WorkerPoolOptions = {
80-
disableWorkerThreads: this._options.disableWorkerThreads || false,
80+
enableWorkerThreads: this._options.enableWorkerThreads || false,
8181
forkOptions: this._options.forkOptions || {},
8282
maxRetries: this._options.maxRetries || 3,
8383
numWorkers: this._options.numWorkers || Math.max(os.cpus().length - 1, 1),

packages/jest-worker/src/types.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ export type FarmOptions = {
7070
workerPath: string,
7171
options?: WorkerPoolOptions,
7272
) => WorkerPoolInterface,
73-
disableWorkerThreads?: boolean,
73+
enableWorkerThreads?: boolean,
7474
};
7575

7676
export type WorkerPoolOptions = {|
7777
setupArgs: Array<mixed>,
7878
forkOptions: ForkOptions,
7979
maxRetries: number,
8080
numWorkers: number,
81-
disableWorkerThreads: boolean,
81+
enableWorkerThreads: boolean,
8282
|};
8383

8484
export type WorkerOptions = {|

0 commit comments

Comments
 (0)