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

fix: ignore all unrelated messages from child process #13543

Merged
merged 3 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- `[expect, @jest/expect]` Provide type of `actual` as a generic argument to `Matchers` to allow better-typed extensions ([#13848](https://github.com/facebook/jest/pull/13848))
- `[jest-circus]` Added explicit mention of test failing because `done()` is not being called in error message ([#13847](https://github.com/facebook/jest/pull/13847))
- `[jest-worker]` Ignore IPC messages not intended for Jest ([#13543](https://github.com/facebook/jest/pull/13543))

### Chore & Maintenance

Expand Down
6 changes: 5 additions & 1 deletion packages/jest-worker/src/workers/ChildProcessWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ export default class ChildProcessWorker
}

private _onMessage(response: ParentMessage) {
// Ignore messages not intended for us
if (!Array.isArray(response)) return;

// TODO: Add appropriate type check
let error: any;

Expand Down Expand Up @@ -311,7 +314,8 @@ export default class ChildProcessWorker
break;

default:
throw new TypeError(`Unexpected response from worker: ${response[0]}`);
// Ignore messages not intended for us
break;
}
}

Expand Down
6 changes: 5 additions & 1 deletion packages/jest-worker/src/workers/NodeThreadsWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ export default class ExperimentalWorker
}

private _onMessage(response: ParentMessage) {
// Ignore messages not intended for us
if (!Array.isArray(response)) return;

let error;

switch (response[0]) {
Expand Down Expand Up @@ -227,7 +230,8 @@ export default class ExperimentalWorker
break;

default:
throw new TypeError(`Unexpected response from worker: ${response[0]}`);
// Ignore messages not intended for us
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ it('creates error instances for known errors', () => {
expect(callback3.mock.calls[0][0]).toBe(412);
});

it('throws when the child process returns a strange message', () => {
it('does not throw when the child process returns a strange message', () => {
const worker = new Worker({
forkOptions: {},
maxRetries: 3,
Expand All @@ -378,9 +378,14 @@ it('throws when the child process returns a strange message', () => {
);

// Type 27 does not exist.
expect(() => {
forkInterface.emit('message', [27]);
}).toThrow(TypeError);
forkInterface.emit('message', [27]);

forkInterface.emit('message', 'test');
forkInterface.emit('message', {foo: 'bar'});
forkInterface.emit('message', 0);
forkInterface.emit('message', null);
forkInterface.emit('message', Symbol('test'));
forkInterface.emit('message', true);
});

it('does not restart the child if it cleanly exited', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ it('creates error instances for known errors', () => {
expect(callback3.mock.calls[0][0]).toBe(412);
});

it('throws when the thread returns a strange message', () => {
it('does not throw when the thread returns a strange message', () => {
const worker = new Worker({
forkOptions: {},
maxRetries: 3,
Expand All @@ -380,10 +380,26 @@ it('throws when the thread returns a strange message', () => {
);

// Type 27 does not exist.
expect(() => {
// @ts-expect-error: Testing internal method
worker._worker.emit('message', [27]);
}).toThrow(TypeError);
// @ts-expect-error: Testing internal method
worker._worker.emit('message', [27]);

// @ts-expect-error: Testing internal method
worker._worker.emit('message', 'test');

// @ts-expect-error: Testing internal method
worker._worker.emit('message', {foo: 'bar'});

// @ts-expect-error: Testing internal method
worker._worker.emit('message', 0);

// @ts-expect-error: Testing internal method
worker._worker.emit('message', null);

// @ts-expect-error: Testing internal method
worker._worker.emit('message', Symbol('test'));

// @ts-expect-error: Testing internal method
worker._worker.emit('message', true);
});

it('does not restart the thread if it cleanly exited', () => {
Expand Down