Skip to content

Commit

Permalink
Progress bar + printing cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer committed Sep 23, 2016
1 parent 835e750 commit 864c3e9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 19 deletions.
9 changes: 5 additions & 4 deletions integration_tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,16 @@ const extractSummary = stdout => {
`);
}

const summary = match[0]
.replace(/\d*\.?\d+m?s/g, '<<REPLACED>>')
.replace(/, estimated <<REPLACED>>/g, '');

const rest = stdout
.slice(0, -match[0].length)
// remove all timestamps
.replace(/\s*\(.*ms\)/gm, '');

return {
summary: match[0].replace(/\d*\.?\d+m?s/g, '<<REPLACED>>'),
rest,
};
return {summary, rest};
};

module.exports = {
Expand Down
15 changes: 11 additions & 4 deletions packages/jest-cli/src/reporters/CoverageReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ const {createReporter} = require('istanbul-api');
const chalk = require('chalk');
const fs = require('fs');
const generateEmptyCoverage = require('../generateEmptyCoverage');
const isCI = require('is-ci');
const istanbulCoverage = require('istanbul-lib-coverage');

const COVERAGE_SUMMARY = chalk.bold;
const FAIL_COLOR = chalk.bold.red;
const RUNNING_TEST_COLOR = chalk.bold.dim;

const isInteractive = process.stdout.isTTY && !isCI;

class CoverageReporter extends BaseReporter {
_coverageMap: CoverageMap;

Expand Down Expand Up @@ -93,9 +96,11 @@ class CoverageReporter extends BaseReporter {

_addUntestedFiles(config: Config, runnerContext: RunnerContext) {
if (config.collectCoverageFrom && config.collectCoverageFrom.length) {
process.stderr.write(RUNNING_TEST_COLOR(
'Running coverage on untested files...',
));
if (isInteractive) {
process.stderr.write(RUNNING_TEST_COLOR(
'Running coverage on untested files...',
));
}
const files = runnerContext.hasteFS.matchFilesWithGlob(
config.collectCoverageFrom,
config.rootDir,
Expand All @@ -118,7 +123,9 @@ class CoverageReporter extends BaseReporter {
}
}
});
this.clearLine();
if (isInteractive) {
this.clearLine();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-cli/src/reporters/VerboseReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class VerboseReporter extends DefaultReporter {

_logLine(str?: string, indentLevel?: number) {
const indentation = ' '.repeat(indentLevel || 0);
process.stderr.write(indentation + (str || '') + '\n');
this._write(indentation + (str || '') + '\n');
}
}

Expand Down
61 changes: 51 additions & 10 deletions packages/jest-cli/src/reporters/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type SummaryOptions = {
width?: number,
};

const PROGRESS_BAR_WIDTH = 40;

const formatTestPath = (config: Config, testPath: Path) => {
const {dirname, basename} = relativePath(config, testPath);
return chalk.gray(dirname + path.sep) + chalk.bold(basename);
Expand All @@ -48,18 +50,24 @@ const getSummary = (
}
const estimatedTime = (options && options.estimatedTime) || 0;
const snapshotResults = aggregatedResults.snapshot;
const width = (options && options.width) || 0;

let suites = chalk.bold('Test Suites: ');
const suitesFailed = aggregatedResults.numFailedTestSuites;
const suitesPassed = aggregatedResults.numPassedTestSuites;
const suitesRun = suitesFailed + suitesPassed;
const suitesTotal = aggregatedResults.numTotalTestSuites;

if (suitesFailed) {
suites += chalk.bold.red(`${suitesFailed} failed`) + ', ';
}

suites += chalk.bold.green(`${suitesPassed} passed`);
suites += ` (${suitesTotal} total)`;
suites +=
chalk.bold.green(`${suitesPassed} passed`) + ', ' +
(suitesRun !== suitesTotal
? suitesRun + '/' + suitesTotal
: suitesTotal
) + ` total`;

let tests = chalk.bold('Tests: ');
const testsPassed = aggregatedResults.numPassedTests;
Expand All @@ -70,8 +78,9 @@ const getSummary = (
tests += chalk.bold.red(`${testsFailed} failed`) + ', ';
}

tests += chalk.bold.green(`${testsPassed} passed`);
tests += ` (${testsTotal} total)`;
tests +=
chalk.bold.green(`${testsPassed} passed`) +
`, ${testsTotal} total`;

let snapshots = chalk.bold('Snapshots: ');
const snapshotsPassed = snapshotResults.matched;
Expand All @@ -92,15 +101,47 @@ const getSummary = (
snapshots += chalk.bold.green(`${snapshotsAdded} added`) + ', ';
}

snapshots += chalk.bold.green(`${snapshotsPassed} passed`);
snapshots += ` (${snapshotsTotal} total)`;
snapshots +=
chalk.bold.green(`${snapshotsPassed} passed`) +
`, ${snapshotsTotal} total`;

const time = renderTime(runTime, estimatedTime, width);
return [suites, tests, snapshots, time].join('\n');
};

let time = chalk.bold(`Time:`) + ` ${runTime}s`;
if (estimatedTime) {
time += ` (${estimatedTime}s estimated)`;
const renderTime = (
runTime,
estimatedTime,
width,
) => {
const renderedTime = (estimatedTime && runTime >= estimatedTime)
? chalk.bold.red(runTime + 's')
: runTime + 's';
let time = chalk.bold(`Time:`) + ` ${renderedTime}`;
if (runTime < estimatedTime) {
time += `, estimated ${estimatedTime}s`;
}

return [suites, tests, snapshots, time].join('\n');
// Only show a progress bar if the test run is actually going to take
// some time.
if (
estimatedTime > 2 &&
runTime < estimatedTime &&
width
) {
const availableWidth = Math.min(PROGRESS_BAR_WIDTH, width);
const length = Math.min(
Math.floor(runTime / estimatedTime * availableWidth),
availableWidth,
);
if (availableWidth >= 2) {
time +=
'\n' +
chalk.green.inverse(' ').repeat(length) +
chalk.white.inverse(' ').repeat(availableWidth - length);
}
}
return time;
};

// wrap a string that contains ANSI escape sequences. ANSI escape sequences
Expand Down

0 comments on commit 864c3e9

Please sign in to comment.