From 161452447a8cbc071447ba92a03efc63da1dd0b7 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 1 Feb 2022 17:44:30 +0200 Subject: [PATCH] refactor(jest-runner): remove unnecessary ProcessTerminatedError logic (#12287) --- packages/jest-runner/package.json | 1 - packages/jest-runner/src/index.ts | 86 +++++++++++++------------------ yarn.lock | 1 - 3 files changed, 36 insertions(+), 52 deletions(-) diff --git a/packages/jest-runner/package.json b/packages/jest-runner/package.json index 62438ab39f10..755d6d8005f3 100644 --- a/packages/jest-runner/package.json +++ b/packages/jest-runner/package.json @@ -25,7 +25,6 @@ "@types/node": "*", "chalk": "^4.0.0", "emittery": "^0.8.1", - "exit": "^0.1.2", "graceful-fs": "^4.2.9", "jest-docblock": "^27.4.0", "jest-environment-jsdom": "^27.4.6", diff --git a/packages/jest-runner/src/index.ts b/packages/jest-runner/src/index.ts index 5f6e17eeb615..2a471a35c493 100644 --- a/packages/jest-runner/src/index.ts +++ b/packages/jest-runner/src/index.ts @@ -7,10 +7,8 @@ import chalk = require('chalk'); import Emittery = require('emittery'); -import exit = require('exit'); import throat from 'throat'; import type { - SerializableError, Test, TestEvents, TestFileEvent, @@ -97,11 +95,10 @@ export default class TestRunner { if (watcher.isInterrupted()) { throw new CancelRun(); } - let sendMessageToJest: TestFileEvent; - // Remove `if(onStart)` in Jest 27 if (onStart) { await onStart(test); + return runTest( test.path, this._globalConfig, @@ -110,41 +107,42 @@ export default class TestRunner { this._context, undefined, ); - } else { - // `deepCyclicCopy` used here to avoid mem-leak - sendMessageToJest = (eventName, args) => - this.eventEmitter.emit( - eventName, - deepCyclicCopy(args, {keepPrototype: false}), - ); - - await this.eventEmitter.emit('test-file-start', [test]); - return runTest( - test.path, - this._globalConfig, - test.context.config, - test.context.resolver, - this._context, - sendMessageToJest, - ); } + + // `deepCyclicCopy` used here to avoid mem-leak + const sendMessageToJest: TestFileEvent = (eventName, args) => + this.eventEmitter.emit( + eventName, + deepCyclicCopy(args, {keepPrototype: false}), + ); + + await this.eventEmitter.emit('test-file-start', [test]); + + return runTest( + test.path, + this._globalConfig, + test.context.config, + test.context.resolver, + this._context, + sendMessageToJest, + ); }) .then(result => { if (onResult) { return onResult(test, result); - } else { - return this.eventEmitter.emit('test-file-success', [ - test, - result, - ]); } + + return this.eventEmitter.emit('test-file-success', [ + test, + result, + ]); }) .catch(err => { if (onFailure) { return onFailure(test, err); - } else { - return this.eventEmitter.emit('test-file-failure', [test, err]); } + + return this.eventEmitter.emit('test-file-failure', [test, err]); }), ), Promise.resolve(), @@ -225,22 +223,6 @@ export default class TestRunner { return promise; }); - const onError = async (err: SerializableError, test: Test) => { - // Remove `if(onFailure)` in Jest 27 - if (onFailure) { - await onFailure(test, err); - } else { - await this.eventEmitter.emit('test-file-failure', [test, err]); - } - if (err.type === 'ProcessTerminatedError') { - console.error( - 'A worker process has quit unexpectedly! ' + - 'Most likely this is an initialization error.', - ); - exit(1); - } - }; - const onInterrupt = new Promise((_, reject) => { watcher.on('change', state => { if (state.interrupted) { @@ -255,14 +237,17 @@ export default class TestRunner { .then(result => { if (onResult) { return onResult(test, result); - } else { - return this.eventEmitter.emit('test-file-success', [ - test, - result, - ]); } + + return this.eventEmitter.emit('test-file-success', [test, result]); }) - .catch(error => onError(error, test)), + .catch(error => { + if (onFailure) { + return onFailure(test, error); + } + + return this.eventEmitter.emit('test-file-failure', [test, error]); + }), ), ); @@ -279,6 +264,7 @@ export default class TestRunner { ); } }; + return Promise.race([runAllTests, onInterrupt]).then(cleanup, cleanup); } diff --git a/yarn.lock b/yarn.lock index 6b062a586442..154844e08e49 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13064,7 +13064,6 @@ __metadata: "@types/source-map-support": ^0.5.0 chalk: ^4.0.0 emittery: ^0.8.1 - exit: ^0.1.2 graceful-fs: ^4.2.9 jest-docblock: ^27.4.0 jest-environment-jsdom: ^27.4.6