Skip to content

Commit 48ddbf4

Browse files
authored
fix: ignore all unrelated messages from child process (#13543)
1 parent 485d8fb commit 48ddbf4

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- `[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))
88
- `[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))
9+
- `[jest-worker]` Ignore IPC messages not intended for Jest ([#13543](https://github.com/facebook/jest/pull/13543))
910

1011
### Chore & Maintenance
1112

packages/jest-worker/src/workers/ChildProcessWorker.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ export default class ChildProcessWorker
255255
}
256256

257257
private _onMessage(response: ParentMessage) {
258+
// Ignore messages not intended for us
259+
if (!Array.isArray(response)) return;
260+
258261
// TODO: Add appropriate type check
259262
let error: any;
260263

@@ -311,7 +314,8 @@ export default class ChildProcessWorker
311314
break;
312315

313316
default:
314-
throw new TypeError(`Unexpected response from worker: ${response[0]}`);
317+
// Ignore messages not intended for us
318+
break;
315319
}
316320
}
317321

packages/jest-worker/src/workers/NodeThreadsWorker.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ export default class ExperimentalWorker
170170
}
171171

172172
private _onMessage(response: ParentMessage) {
173+
// Ignore messages not intended for us
174+
if (!Array.isArray(response)) return;
175+
173176
let error;
174177

175178
switch (response[0]) {
@@ -227,7 +230,8 @@ export default class ExperimentalWorker
227230
break;
228231

229232
default:
230-
throw new TypeError(`Unexpected response from worker: ${response[0]}`);
233+
// Ignore messages not intended for us
234+
break;
231235
}
232236
}
233237

packages/jest-worker/src/workers/__tests__/ChildProcessWorker.test.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ it('creates error instances for known errors', () => {
363363
expect(callback3.mock.calls[0][0]).toBe(412);
364364
});
365365

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

380380
// Type 27 does not exist.
381-
expect(() => {
382-
forkInterface.emit('message', [27]);
383-
}).toThrow(TypeError);
381+
forkInterface.emit('message', [27]);
382+
383+
forkInterface.emit('message', 'test');
384+
forkInterface.emit('message', {foo: 'bar'});
385+
forkInterface.emit('message', 0);
386+
forkInterface.emit('message', null);
387+
forkInterface.emit('message', Symbol('test'));
388+
forkInterface.emit('message', true);
384389
});
385390

386391
it('does not restart the child if it cleanly exited', () => {

packages/jest-worker/src/workers/__tests__/NodeThreadsWorker.test.ts

+21-5
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ it('creates error instances for known errors', () => {
365365
expect(callback3.mock.calls[0][0]).toBe(412);
366366
});
367367

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

382382
// Type 27 does not exist.
383-
expect(() => {
384-
// @ts-expect-error: Testing internal method
385-
worker._worker.emit('message', [27]);
386-
}).toThrow(TypeError);
383+
// @ts-expect-error: Testing internal method
384+
worker._worker.emit('message', [27]);
385+
386+
// @ts-expect-error: Testing internal method
387+
worker._worker.emit('message', 'test');
388+
389+
// @ts-expect-error: Testing internal method
390+
worker._worker.emit('message', {foo: 'bar'});
391+
392+
// @ts-expect-error: Testing internal method
393+
worker._worker.emit('message', 0);
394+
395+
// @ts-expect-error: Testing internal method
396+
worker._worker.emit('message', null);
397+
398+
// @ts-expect-error: Testing internal method
399+
worker._worker.emit('message', Symbol('test'));
400+
401+
// @ts-expect-error: Testing internal method
402+
worker._worker.emit('message', true);
387403
});
388404

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

0 commit comments

Comments
 (0)