Skip to content

Commit

Permalink
Merge pull request #5 from MailOnline/feat/improve-output
Browse files Browse the repository at this point in the history
Feat/improve output
  • Loading branch information
carpasse authored Nov 6, 2017
2 parents 146caae + f36fde3 commit 9262009
Show file tree
Hide file tree
Showing 7 changed files with 392 additions and 76 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,14 @@ npm install --dev jest-tap-reporter
]
}
```

#### Log levels
By default jest-tap-reporter will log the suite path and a resume at the end of the report. If you reduce the report to the bare minimum you can set the reporter logLevel to error.

```javascript
{
"reporters": [
["<rootDir>/node_modules/jest-tap-reporter", {"logLevel": "ERROR"}]
]
}
```
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"homepage": "https://github.com/MailOnline/jest-json-to-tap",
"repository": {
"type": "git",
"url": "[email protected]:MailOnline/jest-json-to-tap.git"
"url": "[email protected]:MailOnline/jest-tap-reporter.git"
},
"license": "MIT",
"devDependencies": {
Expand All @@ -37,10 +37,11 @@
"index.js"
],
"reporters": [
"./"
["./", {"logLevel": "INFO"}]
]
},
"dependencies": {
"chalk": "^2.3.0"
"chalk": "^2.3.0",
"ms": "^2.0.0"
}
}
81 changes: 55 additions & 26 deletions src/TapReporter.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,89 @@
/* eslint-disable id-match, class-methods-use-this, no-console */
const path = require('path');
const chalk = require('chalk');
const ms = require('ms');
const Logger = require('./helpers/Logger');

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

this._globalConfig = globalConfig;
this._options = options;
this._shouldFail = false;
this.logger = new Logger({
logLevel
});
this.counter = 0;

console.log('\n\nStarting ...\n');
this.logger.log('\n');
this.logger.info('\n\n# Starting ...\n');
}

onTestResult (contexts, {testResults}) {
const text = [];
onTestResult (contexts, suite) {
const {testResults, testFilePath} = suite;

if (testFilePath) {
const {dir, base} = path.parse(testFilePath);

this.logger.info(`\n${chalk.grey('#')}${chalk.bgBlue(' SUITE ')} ${chalk.grey(`${dir}${path.sep}`)}${base}`);
}

testResults.forEach((test) => {
this.counter += 1;

testResults.forEach((test, idx) => {
if (test.status === 'passed') {
text.push(`${chalk.green('ok')} ${idx + 1} ${test.title}`);
this.logger.log(`${chalk.green('ok')} ${this.counter} ${test.title}`);
} else if (test.status === 'failed') {
text.push(`${chalk.red('not ok')} ${idx + 1} ${test.title}`);

this.logger.log(`${chalk.red('not ok')} ${this.counter} ${test.title}`);
if (test.failureMessages.length > 0) {
const diagnostics = test.failureMessages
.reduce((lines, msg) => lines.concat(msg.split('\n')), [])
.map((line) => chalk.grey(`# ${line}`))
.join('\n');

text.push(diagnostics);
this.logger.error(diagnostics);
}
} else if (test.status === 'pending') {
text.push(`${chalk.yellow('ok')} ${idx + 1} ${test.title} ${chalk.yellow('# SKIP -')}`);
this.logger.log(`${chalk.yellow('ok')} ${test.title} ${chalk.yellow('# SKIP')}`);
}
});

console.log(text.join('\n'));
}

onRunComplete (contexts, results) {
const text = [];
const format = (msg, color, useColor = true) => {
if (useColor) {
return chalk[color](msg);
}
const {
numFailedTestSuites,
numFailedTests,
numPassedTestSuites,
numPassedTests,
numPendingTestSuites,
numPendingTests,
numTotalTestSuites,
numTotalTests,
startTime
} = results;
const skippedTestSuites = numPendingTestSuites > 0 ? `${chalk.yellow(`${numPendingTestSuites} skipped`)}, ` : '';
const skippedTests = numPendingTests > 0 ? `${chalk.yellow(`${numPendingTests} skipped`)}, ` : '';

return msg;
};
this._shouldFail = numFailedTestSuites > 0 || numFailedTests > 0;

this._shouldFail = results.numFailedTestSuites > 0 || results.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`);
}

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`);
}

text.push(format(chalk.grey(`# Total tests: ${results.numTotalTests}\n`), this._shouldFail ? 'bgRed' : 'bgGreen'));
text.push(format(`# Passed suites: ${results.numPassedTestSuites}`, 'green', !this._shouldFail));
text.push(format(`# Failed suites: ${results.numFailedTestSuites}`, 'red', this._shouldFail));
text.push(format(`# Passed tests: ${results.numPassedTests}`, 'green', !this._shouldFail));
text.push(format(`# Failed tests: ${results.numFailedTests}`, 'red', this._shouldFail));
text.push(format(`# Skipped tests: ${results.numPendingTests}`, 'yellow', results.numPendingTests > 0));
this.logger.info(`# time: ${ms(Date.now() - startTime)}`);
this.logger.info('\n');

console.log(`\n${text.join('\n')}`);
this.counter = 0;
}

getLastError () {
Expand Down
50 changes: 50 additions & 0 deletions src/helpers/Logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* eslint-disable sort-keys */
const LEVELS = {
ERROR: 1,
WARN: 2,
INFO: 3
};
/* eslint-enable sort-keys */

// eslint-disable-next-line no-console
const DEFAULT_LOG = (...args) => console.log(...args);
const level = Symbol('level');

class Logger {
constructor ({log = DEFAULT_LOG, logLevel} = {}) {
this.log = log;
this.setLevel(logLevel || 'INFO');
}

setLevel (newLevel) {
if (typeof LEVELS[newLevel] === 'undefined') {
throw new TypeError('Unknown level');
}

this[level] = LEVELS[newLevel];
}

getLevel () {
return Object.keys(LEVELS).filter((key) => LEVELS[key] === this[level])[0];
}

info (...args) {
if (this[level] >= LEVELS.INFO) {
this.log(...args);
}
}

warn (...args) {
if (this[level] >= LEVELS.WARN) {
this.log(...args);
}
}

error (...args) {
if (this[level] >= LEVELS.ERROR) {
this.log(...args);
}
}
}

module.exports = Logger;
Loading

0 comments on commit 9262009

Please sign in to comment.