Skip to content

Commit

Permalink
feat: format test summary as jest
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Dalecky authored and Vadim Dalecky committed Nov 7, 2017
1 parent d75e1be commit cc6b648
Showing 1 changed file with 65 additions and 22 deletions.
87 changes: 65 additions & 22 deletions src/TapReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,52 @@ const STATUS_PENDING = 'pending';
const REG_TRACE_LINE = /\s*(.+)\((.+):([0-9]+):([0-9]+)\)$/;
const MDASH = '\u2014';

class PrettyLineWriter {
constructor (logger) {
this.logger = logger;
}

blank () {
this.logger.info('\n');
}

comment (line) {
this.logger.info(chalk`{hidden #} ${line}`);
}

commentLight (line) {
this.comment(chalk`{dim ${line}}`);
}

keyValue (key, value) {
// eslint-disable-next-line no-use-extend-native/no-use-extend-native
const keyFormatted = (key + ':').padEnd(12, ' ');

this.comment(chalk`{black.bold ${keyFormatted}} ${value}`);
}

stats (name, failed, skipped, passed, total) {
let value = '';

if (total) {
value += chalk`{red.bold ${failed} failed}`;

if (skipped) {
value += chalk`, {yellow.bold ${skipped} skipped}`;
}

value += chalk`, {green.bold ${passed} passed}`;
}

value += `${total ? ', ' : ''}${total} total`;

this.keyValue(name, value);
}
}

class TapReporter {
constructor (globalConfig = {}, options = {}) {
console.log(globalConfig);
const {logLevel = 'INFO'} = options;

this._globalConfig = globalConfig;
Expand All @@ -22,9 +66,12 @@ class TapReporter {
this.logger = new Logger({
logLevel
});
this.line = new PrettyLineWriter(this.logger);
this.counter = 0;
this.logger.info(chalk`\n\n{hidden #} {green Starting...}`);
this.onAssertionResult = this.onAssertionResult.bind(this);

this.line.blank();
this.line.comment(chalk`{green Starting...}`);
}

pathRelativeToRoot (filePath) {
Expand Down Expand Up @@ -120,8 +167,8 @@ class TapReporter {
const {dir, base} = path.parse(testFilePath);
const prefix = this._watch ? '' : '\n';
const label = numFailingTests > 0 ?
chalk`{bgRed.bold.rgb(255,255,255) FAIL }` :
chalk`{bgGreen.bold.rgb(255,255,255) PASS }`;
chalk`{bgRed.rgb(255,255,255).bold FAIL }` :
chalk`{bgGreen.rgb(255,255,255).bold PASS }`;
const tapLine = chalk`${prefix}{hidden #} ${label} {grey ${this.pathRelativeToRoot(dir)}${path.sep}}{bold ${base}}`;

this.logger.info(tapLine + '\n');
Expand All @@ -130,6 +177,11 @@ class TapReporter {
testResults.forEach(this.onAssertionResult);
}

onRunStart (results, options) {
this.onRunStartResults = results;
this.onRunStartOptions = options;
}

onRunComplete (contexts, results) {
const {
numFailedTestSuites,
Expand All @@ -140,30 +192,21 @@ class TapReporter {
numPendingTests,
numTotalTestSuites,
numTotalTests,
snapshot,
startTime
} = results;
const skippedTestSuites = numPendingTestSuites > 0 ? `${chalk.yellow(`${numPendingTestSuites} skipped`)}, ` : '';
const skippedTests = numPendingTests > 0 ? `${chalk.yellow(`${numPendingTests} skipped`)}, ` : '';

this._shouldFail = numFailedTestSuites > 0 || numFailedTests > 0;

this.logger.info('\n');
if (numFailedTestSuites > 0) {
this.logger.info(`# testSuites: ${skippedTestSuites}${chalk.red(`${numFailedTestSuites} failed`)}, ${numTotalTestSuites} total`);
} else {
this.logger.info(`# testSuites: ${skippedTestSuites}${chalk.green(`${numPassedTestSuites} passed`)}, ${numTotalTestSuites} total`);
}
const {estimatedTime} = this.onRunStartOptions;

if (numFailedTests > 0) {
this.logger.info(`# tests: ${skippedTests}${chalk.red(`${numFailedTests} failed`)}, ${numTotalTests} total`);
} else {
this.logger.info(`# tests: ${skippedTests}${chalk.green(`${numPassedTests} passed`)}, ${numTotalTests} total`);
}

this.logger.info(`# time: ${ms(Date.now() - startTime)}`);
this.logger.info('\n');
this._shouldFail = numFailedTestSuites > 0 || numFailedTests > 0;

this.counter = 0;
this.line.blank();
this.line.stats('Test Suites', numFailedTestSuites, numPendingTestSuites, numPassedTestSuites, numTotalTestSuites);
this.line.stats('Tests', numFailedTests, numPendingTests, numPassedTests, numTotalTests);
this.line.stats('Snapshots', snapshot.total - snapshot.matched, 0, snapshot.matched, snapshot.total);
this.line.keyValue('Time', `${((Date.now() - startTime) / 1e3).toFixed(3)}s, estimated ${estimatedTime}s`);
this.line.commentLight('Ran all test suites.');
this.line.blank();
}

getLastError () {
Expand Down

0 comments on commit cc6b648

Please sign in to comment.