Skip to content

Commit

Permalink
Merge pull request #15 from MailOnline/feat/progress
Browse files Browse the repository at this point in the history
Feat/progress
  • Loading branch information
streamich authored Nov 23, 2017
2 parents a2275ce + 74427d8 commit 73f915f
Show file tree
Hide file tree
Showing 24 changed files with 713 additions and 322 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": "mailonline",
"extends": [
"mailonline",
"mailonline/jest"
],
"rules": {
"import/unambiguous": "off",
"import/no-commonjs": "off"
Expand Down
37 changes: 21 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@
[npm]: https://www.npmjs.com/package/jest-tap-reporter
[license-badge]: https://img.shields.io/badge/license-MIT-orange.svg
[license]: ./LICENSE
[tap]: https://testanything.org/tap-specification.html
[jest]: https://facebook.github.io/jest/

[![jest-tap-reporter on NPM][npm-badge]][npm] [![Travis CI][travis-badge]][travis] [![License][license-badge]][license]

Jest reporter that outputs valid [TAP](https://testanything.org/tap-specification.html) output and highlights similar to Jest's default reporter.
[TAP][tap] reporter for [Jest][jest].


Mac terminal sample output:

![jest-tap-reporter exaple Mac](./docs/example-mac.png)

VS Code sample output:

![jest-tap-reporter exaple VS Code](./docs/example-vscode.png)
- Outputs valid TAP
- Highlights similar to Jest default reporter, see [Mac](./docs/example-mac.png) and [VS Code](./docs/example-vscode.png) examples
- [Highlights line and column of errors](./docs/highlight.png)
- [Shows progress](./docs/progress.png) while running tests

## Installation

#### yarn

```shell
yarn add --dev jest-tap-reporter
```

#### npm

```shell
npm install --dev jest-tap-reporter
```

## Usage

#### Add to your jest configuration
#### Add to your Jest configuration

```javascript
{
Expand All @@ -46,21 +46,26 @@ npm install --dev jest-tap-reporter
}
```

#### Log levels
#### Options

By default jest-tap-reporter uses `INFO` log level, which will log the suite path and a summary at the end of a test run.
If you want to reduce the reporting to bare minimum you can set the `logLevel` parameter to `ERROR`.
You can add an optional configuration object:

```javascript
{
"reporters": [
["jest-tap-reporter", {"logLevel": "ERROR"}]
["jest-tap-reporter", {
"logLevel": "ERROR",
"showInternalStackTraces": true
}]
]
}
```

Available log levels are: `ERROR`, `WARN`, `INFO`.
Options:

- `logLevel` - specifies the log level. By default jest-tap-reporter uses `INFO` log level, which will log the suite path and a summary at the end of a test run. If you want to reduce the reporting to bare minimum you can set the `logLevel` parameter to `ERROR`. available log levels are: `ERROR`, `WARN`, `INFO`.
- `showInternalStackTraces` - shows stack traces from *"internal"* folders, like `/node_modules` and `/internal`, defaults to `false`.

## License

MIT, see [LICENSE](./LICENSE).
[MIT](./LICENSE).
Binary file added docs/highlight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/progress.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"test": "jest",
"demo": "jest --testPathPattern 'demo/.+\\.test\\.js' --testRegex 'demo/.+\\.test\\.js'",
"demo": "jest --testPathPattern 'demo/.+\\.test\\.js' --testRegex 'demo/.+\\.test\\.js$'",
"lint": "eslint --ignore-path .gitignore '**/*.js'",
"precommit": "npm run lint",
"prepush": "npm test",
Expand Down Expand Up @@ -38,9 +38,12 @@
"index.js"
],
"reporters": [
["./", {"logLevel": "INFO"}]
["./", {
"logLevel": "INFO",
"showInternalStackTraces": false
}]
],
"testRegex": "test\\/.+\\.(test|spec)\\.jsx?$"
"testRegex": "(test|src)\\/.+\\.(test|spec)\\.jsx?$"
},
"dependencies": {
"chalk": "^2.3.0",
Expand Down
75 changes: 60 additions & 15 deletions src/LineWriter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable complexity, no-use-extend-native/no-use-extend-native */
const path = require('path');
const chalk = require('chalk');
const bar = require('utf8-bar');
const formatComment = require('./format/formatComment');
const formatCodeFrame = require('./format/formatCodeFrame');
const formatStatsBar = require('./format/formatStatsBar');
const formatFailureMessageTraceLine = require('./format/formatFailureMessageTraceLine');
Expand Down Expand Up @@ -28,8 +30,6 @@ const PASS = chalk.supportsColor ?
chalk`{reset.inverse.bold.green ${PASS_TEXT} }` :
` ${PASS_TEXT} `;

const formatComment = (line) => chalk`{hidden #} ${line}`;

class LineWriter {
constructor (logger, root) {
this.counter = 0;
Expand All @@ -44,12 +44,16 @@ class LineWriter {
return this.counter;
}

info (line) {
this.logger.info(line);
}

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

comment (line) {
this.logger.info(formatComment(line));
this.info(formatComment(line));
}

commentBlock (str) {
Expand Down Expand Up @@ -95,9 +99,9 @@ class LineWriter {
const list = [];

if (total) {
const bar = formatStatsBar(passed / total, passed + skipped < total);
const formattedBar = formatStatsBar(passed / total, Boolean(failed));

list.push(bar);
list.push(formattedBar);

if (failed) {
list.push(chalk`{red.bold ${failed} failed}`);
Expand Down Expand Up @@ -125,9 +129,9 @@ class LineWriter {
const list = [];

const percent = passed / total;
const bar = formatStatsBar(percent, percent < 1 && !updated && !added);
const formattedStatsBar = formatStatsBar(percent, percent < 1 && !updated && !added);

list.push(bar);
list.push(formattedStatsBar);

if (failed) {
list.push(chalk`{red.bold ${failed} failed}`);
Expand Down Expand Up @@ -170,7 +174,7 @@ class LineWriter {
return path.relative(this.root, filePath);
}

formatFailureMessage (message) {
formatFailureMessage (message, showInternalStackTraces) {
const [firstLine, ...lines] = message.split('\n');
const outputLines = [];
let context = '';
Expand Down Expand Up @@ -221,7 +225,9 @@ class LineWriter {

// eslint-disable-next-line no-lonely-if
if (internalsStarted) {
pushTraceLineDim(formatFailureMessageTraceLine(description, relativeFilePath, row, column));
if (showInternalStackTraces) {
pushTraceLineDim(formatFailureMessageTraceLine(description, relativeFilePath, row, column));
}
} else {
pushTraceLine(formatFailureMessageTraceLine(description, relativeFilePath, row, column));

Expand All @@ -240,7 +246,9 @@ class LineWriter {
if (atPathMatches) {
const [, atPathPath, atPathRow, atPathColumn] = atPathMatches;

pushMethod(chalk`at {cyan ${this.getPathRelativeToRoot(atPathPath)}}:{bold ${atPathRow}}:{bold ${atPathColumn}}`);
if (!internalsStarted || showInternalStackTraces) {
pushMethod(chalk`at {cyan ${this.getPathRelativeToRoot(atPathPath)}}:{bold ${atPathRow}}:{bold ${atPathColumn}}`);
}
} else {
pushMethod(line);
}
Expand Down Expand Up @@ -275,17 +283,15 @@ class LineWriter {
}
}

push('');

return outputLines.map((line) => formatComment(whitespace + line)).join('\n');
}

errors (messages) {
errors (messages, showInternalStackTraces) {
if (!messages.length) {
return;
}

const formattedMessages = messages.map((message) => this.formatFailureMessage(message)).join('\n');
const formattedMessages = messages.map((message) => this.formatFailureMessage(message, showInternalStackTraces)).join('\n');

this.logger.error(formattedMessages);
}
Expand All @@ -304,6 +310,45 @@ class LineWriter {
this.logger.log(chalk`{reset.inverse 1..${count}}`);
this.planWritten = true;
}

aggregatedResults (aggregatedResults, estimatedTime) {
const snapshotResults = aggregatedResults.snapshot;
const snapshotsAdded = snapshotResults.added;
const snapshotsFailed = snapshotResults.unmatched;
const snapshotsPassed = snapshotResults.matched;
const snapshotsTotal = snapshotResults.total;
const snapshotsUpdated = snapshotResults.updated;
const suitesFailed = aggregatedResults.numFailedTestSuites;
const suitesPassed = aggregatedResults.numPassedTestSuites;
const suitesPending = aggregatedResults.numPendingTestSuites;
const suitesTotal = aggregatedResults.numTotalTestSuites;
const testsFailed = aggregatedResults.numFailedTests;
const testsPassed = aggregatedResults.numPassedTests;
const testsPending = aggregatedResults.numPendingTests;
const testsTotal = aggregatedResults.numTotalTests;
const startTime = aggregatedResults.startTime;

this.stats('Test Suites', suitesFailed, suitesPending, suitesPassed, suitesTotal);
this.stats('Tests', testsFailed, testsPending, testsPassed, testsTotal);
if (snapshotsTotal) {
this.snapshots(snapshotsFailed, snapshotsUpdated, snapshotsAdded, snapshotsPassed, snapshotsTotal);
}

const timeValue = `${((Date.now() - startTime) / 1e3).toFixed(3)}s` + (estimatedTime ? `, estimated ${estimatedTime}s` : '');

this.keyValue('Time', timeValue);
}

timeProgressBar (percentage) {
if (percentage > 1) {
return;
}

const line = bar(this.logger.stream.columns, percentage);
const lineFormatted = chalk`{grey.dim ${line}}`;

this.logger.write(lineFormatted);
}
}

module.exports = LineWriter;
Loading

0 comments on commit 73f915f

Please sign in to comment.