Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Total-test files via add-test event to jest-reporter #32

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/jest-core/src/ReporterDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ export default class ReporterDispatcher {
testResult.console = undefined;
}

async onTestFileAdd(test: Test): Promise<void> {
for (const reporter of this._reporters) {
if (reporter.onTestFileAdd) {
await reporter.onTestFileAdd(test);
}
}
}

async onTestFileStart(test: Test): Promise<void> {
for (const reporter of this._reporters) {
if (reporter.onTestFileStart) {
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-core/src/TestScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ export default class TestScheduler {

const runInBand = shouldRunInBand(tests, timings, this._globalConfig);

const onTestFileAdd = async (test: TestRunner.Test) => {
await this._dispatcher.onTestFileAdd(test);
};

const onResult = async (test: TestRunner.Test, testResult: TestResult) => {
if (watcher.isInterrupted()) {
return Promise.resolve();
Expand Down Expand Up @@ -224,6 +228,7 @@ export default class TestScheduler {
*/
if (testRunner.__PRIVATE_UNSTABLE_API_supportsEventEmitters__) {
const unsubscribes = [
testRunner.on('test-file-add', ([test]) => onTestFileAdd(test)),
testRunner.on('test-file-start', ([test]) =>
onTestFileStart(test),
),
Expand Down
15 changes: 13 additions & 2 deletions packages/jest-reporters/src/Status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ export default class Status {
private _estimatedTime: number;
private _interval?: NodeJS.Timeout;
private _aggregatedResults?: AggregatedResult;
private _testFilesAdded: Array<Test>;
private _showStatus: boolean;

constructor() {
this._cache = null;
this._currentTests = new CurrentTestList();
this._testFilesAdded = [];
this._currentTestCases = [];
this._done = false;
this._emitScheduled = false;
Expand Down Expand Up @@ -117,6 +119,15 @@ export default class Status {
this._emit();
}

addTestFiles(test: Test): void {
this._testFilesAdded.push(test);
if (!this._showStatus) {
this._emit();
} else {
this._debouncedEmit();
}
}

addTestCaseResult(test: Test, testCaseResult: TestCaseResult): void {
this._currentTestCases.push({test, testCaseResult});
if (!this._showStatus) {
Expand Down Expand Up @@ -181,10 +192,10 @@ export default class Status {
}
});

if (this._showStatus && this._aggregatedResults) {
if (this._showStatus && this._aggregatedResults && this._testFilesAdded) {
content +=
'\n' +
getSummary(this._aggregatedResults, {
getSummary(this._aggregatedResults, this._testFilesAdded, {
currentTestCases: this._currentTestCases,
estimatedTime: this._estimatedTime,
roundTime: true,
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-reporters/src/default_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ export default class DefaultReporter extends BaseReporter {
this._status.runStarted(aggregatedResults, options);
}

onTestFileAdd(test: Test): void {
this._status.addTestFiles(test);
}

onTestStart(test: Test): void {
this._status.testStarted(test.path, test.context.config);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-reporters/src/summary_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default class SummaryReporter extends BaseReporter {
);

if (numTotalTestSuites) {
let message = getSummary(aggregatedResults, {
let message = getSummary(aggregatedResults, undefined, {
estimatedTime: this._estimatedTime,
});

Expand Down
1 change: 1 addition & 0 deletions packages/jest-reporters/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type OnTestFailure = (
export type OnTestSuccess = (test: Test, result: TestResult) => Promise<any>;

export interface Reporter {
readonly onTestFileAdd?: (test: Test) => Promise<void> | void;
readonly onTestResult?: (
test: Test,
testResult: TestResult,
Expand Down
7 changes: 6 additions & 1 deletion packages/jest-reporters/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ const getValuesCurrentTestCases = (

export const getSummary = (
aggregatedResults: AggregatedResult,
testFilesAdded?: Array<Test>,
options?: SummaryOptions,
): string => {
let runTime = (Date.now() - aggregatedResults.startTime) / 1000;
Expand Down Expand Up @@ -161,6 +162,7 @@ export const getSummary = (
const testsPassed = aggregatedResults.numPassedTests;
const testsPending = aggregatedResults.numPendingTests;
const testsTodo = aggregatedResults.numTodoTests;
const testsTotalAdded = testFilesAdded?.length;
const testsTotal = aggregatedResults.numTotalTests;
const width = (options && options.width) || 0;

Expand Down Expand Up @@ -200,7 +202,10 @@ export const getSummary = (
(updatedTestsPassed > 0
? chalk.bold.green(`${updatedTestsPassed} passed`) + ', '
: '') +
`${updatedTestsTotal} total`;
(testsTotalAdded !== updatedTestsTotal
? testsTotalAdded + ' of' + updatedTestsTotal
: updatedTestsTotal) +
` total`;

const snapshots =
chalk.bold('Snapshots: ') +
Expand Down
1 change: 1 addition & 0 deletions packages/jest-runner/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type OnTestSuccess = (

// Typings for `sendMessageToJest` events
export type TestEvents = {
'test-file-add': [Test];
'test-file-start': [Test];
'test-file-success': [Test, TestResult];
'test-file-failure': [Test, SerializableError];
Expand Down