Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kunal-kushwaha committed Jun 22, 2020
1 parent 4471bbb commit 5adeefc
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/jest-circus/src/eventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ const eventHandler: Circus.EventHandler = (
case 'test_done': {
event.test.duration = getTestDuration(event.test);
event.test.status = 'done';
state.parentProcess.emit('test_done', {test: event.test});
state.currentlyRunningTest = null;
break;
}
Expand Down
9 changes: 9 additions & 0 deletions packages/jest-core/src/ReporterDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type {AggregatedResult, TestResult} from '@jest/test-result';
import type {Test} from 'jest-runner';
import type {Context} from 'jest-runtime';
import type {Reporter, ReporterOnStartOptions} from '@jest/reporters';
import type {TestEntry} from '@jest/types/build/Circus';


export default class ReporterDispatcher {
private _reporters: Array<Reporter>;
Expand All @@ -27,6 +29,13 @@ export default class ReporterDispatcher {
);
}

async onIndividualTestResult(testEntry: TestEntry) {
for (const reporter of this._reporters) {
reporter.onIndividualTestResult &&
(await reporter.onIndividualTestResult(testEntry));
}
}

async onTestResult(
test: Test,
testResult: TestResult,
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-core/src/TestScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export default class TestScheduler {
tests: Array<TestRunner.Test>,
watcher: TestWatcher,
): Promise<AggregatedResult> {
const onIndividualTestResult = this._dispatcher.onIndividualTestResult.bind(
this._dispatcher,
);
const onStart = this._dispatcher.onTestStart.bind(this._dispatcher);
const timings: Array<number> = [];
const contexts = new Set<Context>();
Expand Down Expand Up @@ -199,6 +202,7 @@ export default class TestScheduler {
onStart,
onResult,
onFailure,
onIndividualTestResult,
{
serial: runInBand || Boolean(testRunners[runner].isSerial),
},
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-reporters/src/base_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import type {AggregatedResult, TestResult} from '@jest/test-result';
import {preRunMessage} from 'jest-util';
import type {Context, Reporter, ReporterOnStartOptions, Test} from './types';
import type {TestEntry} from '@jest/types/build/Circus';

const {remove: preRunMessageRemove} = preRunMessage;

Expand All @@ -25,6 +26,10 @@ export default class BaseReporter implements Reporter {
preRunMessageRemove(process.stderr);
}

onIndividualTestResult(
_testEntry: TestEntry
): void {}

onTestResult(
_test?: Test,
_testResult?: TestResult,
Expand Down
17 changes: 15 additions & 2 deletions packages/jest-runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,25 @@ class TestRunner {
onResult: JestOnTestSuccess,
onFailure: JestOnTestFailure,
options: JestTestRunnerOptions,
onIndividualTestResult: any,
): Promise<void> {
return await (options.serial
? this._createInBandTestRun(tests, watcher, onStart, onResult, onFailure)
? this._createInBandTestRun(
tests,
watcher,
onStart,
onResult,
onFailure,
onIndividualTestResult,
)
: this._createParallelTestRun(
tests,
watcher,
onStart,
onResult,
onFailure,
));
onIndividualTestResult,
));
}

private async _createInBandTestRun(
Expand All @@ -79,7 +88,9 @@ class TestRunner {
onStart: JestOnTestStart,
onResult: JestOnTestSuccess,
onFailure: JestOnTestFailure,
onIndividualTestResult: any,
) {
process.on('test_done', evt => onIndividualTestResult(evt.test));
process.env.JEST_WORKER_ID = '1';
const mutex = throat(1);
return tests.reduce(
Expand Down Expand Up @@ -113,6 +124,7 @@ class TestRunner {
onStart: JestOnTestStart,
onResult: JestOnTestSuccess,
onFailure: JestOnTestFailure,
onIndividualTestResult: any,
) {
const resolvers: Map<string, SerializableResolver> = new Map();
for (const test of tests) {
Expand All @@ -129,6 +141,7 @@ class TestRunner {
forkOptions: {stdio: 'pipe'},
maxRetries: 3,
numWorkers: this._globalConfig.maxWorkers,
onIndividualTestResult,
setupArgs: [
{
serializableResolvers: Array.from(resolvers.values()),
Expand Down
1 change: 1 addition & 0 deletions packages/jest-worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export default class JestWorker {
maxRetries: this._options.maxRetries || 3,
numWorkers: this._options.numWorkers || Math.max(cpus().length - 1, 1),
setupArgs: this._options.setupArgs || [],
onIndividualTestResult: options.onIndividualTestResult,
};

if (this._options.WorkerPool) {
Expand Down
13 changes: 13 additions & 0 deletions packages/jest-worker/src/workers/processChild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ const messageListener = (request: any) => {
}
};
process.on('message', messageListener);
process.on('test_done', ({test}) => {
process.send([
'test_done',
{
name: test.name,
duration: test.duration,
parent: test.parent && {
name: test.parent.name,
parent: test.parent.parent && test.parent.parent.name,
},
},
]);
});

function reportSuccess(result: any) {
if (!process || !process.send) {
Expand Down
13 changes: 13 additions & 0 deletions packages/jest-worker/src/workers/threadChild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ const messageListener = (request: any) => {
}
};
parentPort!.on('message', messageListener);
process.on('test_done', ({test}) => {
process.send([
'test_done',
{
name: test.name,
duration: test.duration,
parent: test.parent && {
name: test.parent.name,
parent: test.parent.parent && test.parent.parent.name,
},
},
]);
});

function reportSuccess(result: any) {
if (isMainThread) {
Expand Down

0 comments on commit 5adeefc

Please sign in to comment.