Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jest-worker] Don't pass explicit env to new Worker when using worker_threads #12141

Merged
merged 3 commits into from Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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