Skip to content

Commit

Permalink
refactor(jest-runner): remove unnecessary ProcessTerminatedError logic (
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas committed Feb 1, 2022
1 parent 9edcaaf commit 1614524
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 52 deletions.
1 change: 0 additions & 1 deletion packages/jest-runner/package.json
Expand Up @@ -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",
Expand Down
86 changes: 36 additions & 50 deletions packages/jest-runner/src/index.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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(),
Expand Down Expand Up @@ -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) {
Expand All @@ -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]);
}),
),
);

Expand All @@ -279,6 +264,7 @@ export default class TestRunner {
);
}
};

return Promise.race([runAllTests, onInterrupt]).then(cleanup, cleanup);
}

Expand Down
1 change: 0 additions & 1 deletion yarn.lock
Expand Up @@ -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
Expand Down

0 comments on commit 1614524

Please sign in to comment.