diff --git a/CHANGELOG.md b/CHANGELOG.md index 055d9914cf63..b889faa98471 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - `[jest-config]` Add config validation for `projects` option ([#13565](https://github.com/facebook/jest/pull/13565)) - `[jest-mock]` Treat cjs modules as objects so they can be mocked ([#13513](https://github.com/facebook/jest/pull/13513)) - `[jest-worker]` Throw an error instead of hanging when jest workers terminate unexpectedly ([#13566](https://github.com/facebook/jest/pull/13566)) +- `[jest-worker]` Ignore IPC messages not intended for Jest ([#13543](https://github.com/facebook/jest/pull/13543)) ### Chore & Maintenance diff --git a/packages/jest-worker/src/workers/ChildProcessWorker.ts b/packages/jest-worker/src/workers/ChildProcessWorker.ts index 691bbb6edefd..9e0832c1e7fa 100644 --- a/packages/jest-worker/src/workers/ChildProcessWorker.ts +++ b/packages/jest-worker/src/workers/ChildProcessWorker.ts @@ -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; @@ -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; } } diff --git a/packages/jest-worker/src/workers/NodeThreadsWorker.ts b/packages/jest-worker/src/workers/NodeThreadsWorker.ts index 2acf0054ae5d..3a9f86a46be0 100644 --- a/packages/jest-worker/src/workers/NodeThreadsWorker.ts +++ b/packages/jest-worker/src/workers/NodeThreadsWorker.ts @@ -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]) { @@ -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; } } diff --git a/packages/jest-worker/src/workers/__tests__/ChildProcessWorker.test.ts b/packages/jest-worker/src/workers/__tests__/ChildProcessWorker.test.ts index 9e6b513cb22f..90c9e1b07bdd 100644 --- a/packages/jest-worker/src/workers/__tests__/ChildProcessWorker.test.ts +++ b/packages/jest-worker/src/workers/__tests__/ChildProcessWorker.test.ts @@ -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, @@ -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', () => { diff --git a/packages/jest-worker/src/workers/__tests__/NodeThreadsWorker.test.ts b/packages/jest-worker/src/workers/__tests__/NodeThreadsWorker.test.ts index 8d633af24e97..7e0e53dc9d50 100644 --- a/packages/jest-worker/src/workers/__tests__/NodeThreadsWorker.test.ts +++ b/packages/jest-worker/src/workers/__tests__/NodeThreadsWorker.test.ts @@ -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, @@ -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', () => {