Skip to content

Commit

Permalink
[jest-worker] Don't pass explicit env to new Worker when using `w…
Browse files Browse the repository at this point in the history
…orker_threads` (#12141)
  • Loading branch information
nicolo-ribaudo committed Dec 13, 2021
1 parent 07c2c15 commit 7148872
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,12 @@

### Performance

## 27.4.5

### Fixes

- `[jest-worker]` Stop explicitly passing `process.env` ([#12141](https://github.com/facebook/jest/pull/12141))

## 27.4.4

### Fixes
Expand Down
5 changes: 1 addition & 4 deletions packages/jest-worker/src/workers/NodeThreadsWorker.ts
Expand Up @@ -60,10 +60,6 @@ export default class ExperimentalWorker implements WorkerInterface {

initialize(): void {
this._worker = new Worker(path.resolve(__dirname, './threadChild.js'), {
env: {
...process.env,
JEST_WORKER_ID: String(this._options.workerId + 1), // 0-indexed workerId, 1-indexed JEST_WORKER_ID
},
eval: false,
// @ts-expect-error: added in newer versions
resourceLimits: this._options.resourceLimits,
Expand Down Expand Up @@ -101,6 +97,7 @@ export default class ExperimentalWorker implements WorkerInterface {
false,
this._options.workerPath,
this._options.setupArgs,
String(this._options.workerId + 1), // 0-indexed workerId, 1-indexed JEST_WORKER_ID
]);

this._retries++;
Expand Down
Expand Up @@ -70,7 +70,6 @@ it('passes fork options down to worker_threads.Worker, adding the defaults', ()

expect(workerThreads.mock.calls[0][0]).toBe(thread.replace(/\.ts$/, '.js'));
expect(workerThreads.mock.calls[0][1]).toEqual({
env: process.env, // Default option.
eval: false,
execArgv: ['--inspect', '-p'],
execPath: 'hello', // Added option.
Expand All @@ -84,23 +83,12 @@ it('passes fork options down to worker_threads.Worker, adding the defaults', ()
});
});

it('passes workerId to the thread and assign it to env.JEST_WORKER_ID', () => {
// eslint-disable-next-line no-new
new Worker({
forkOptions: {},
maxRetries: 3,
workerId: 2,
workerPath: '/tmp/foo',
});

expect(workerThreads.mock.calls[0][1].env.JEST_WORKER_ID).toEqual('3');
});

it('initializes the thread with the given workerPath', () => {
it('initializes the thread with the given workerPath and workerId', () => {
const worker = new Worker({
forkOptions: {},
maxRetries: 3,
setupArgs: ['foo', 'bar'],
workerId: 2,
workerPath: '/tmp/foo/bar/baz.js',
});

Expand All @@ -109,6 +97,7 @@ it('initializes the thread with the given workerPath', () => {
false,
'/tmp/foo/bar/baz.js',
['foo', 'bar'],
'3',
]);
});

Expand Down
12 changes: 12 additions & 0 deletions packages/jest-worker/src/workers/__tests__/threadChild.test.js
Expand Up @@ -128,6 +128,18 @@ afterEach(() => {
thread.removeAllListeners('message');
});

it('sets env.JEST_WORKER_ID', () => {
thread.emit('message', [
CHILD_MESSAGE_INITIALIZE,
true, // Not really used here, but for flow type purity.
'./my-fancy-worker',
[],
'3',
]);

expect(process.env.JEST_WORKER_ID).toBe('3');
});

it('lazily requires the file', () => {
expect(mockCount).toBe(0);

Expand Down
1 change: 1 addition & 0 deletions packages/jest-worker/src/workers/threadChild.ts
Expand Up @@ -41,6 +41,7 @@ const messageListener = (request: any) => {
const init: ChildMessageInitialize = request;
file = init[2];
setupArgs = request[3];
process.env.JEST_WORKER_ID = request[4];
break;

case CHILD_MESSAGE_CALL:
Expand Down

0 comments on commit 7148872

Please sign in to comment.