Skip to content

Commit

Permalink
Merge pull request #44 from MailOnline/pr/42
Browse files Browse the repository at this point in the history
Pr/42
  • Loading branch information
streamich authored Dec 13, 2017
2 parents 84091db + 87d4a9c commit 8dbb637
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 45 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ coverage/
dev/
node_modules/
yarn.lock
package-lock.json
package-lock.json
test.tap
47 changes: 37 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,60 @@ npm install --dev jest-tap-reporter

#### Add to your Jest configuration

In `package.json` file:

```javascript
{
"reporters": [
"jest-tap-reporter"
]
"jest": {
"reporters": [
"jest-tap-reporter"
]
}
}
```

#### Options

You can add an optional configuration object:

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

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`.
- `filePath` - specifies a file to write the results. If not supplied it will use `process.stdout`.
- `showHeader` - whether to show starting message on startup, defaults to `true`.
- `showInternalStackTraces` - shows stack traces from *"internal"* folders, like `/node_modules` and `/internal`, defaults to `false`.
- `showProgress` - whether to not show intermediate test result summary while testing is in progress. In general, defaults to `true`. When writing to file or in CI environment, it is forced to be `false`.

#### Example: writing to file

You can write test results to a file with the following config:

```js
{
"jest": {
"reporters": [
["jest-tap-reporter", {
"logLevel": "ERROR",
"filePath": "test.tap"
}]
],
}
}
```

## License

Expand Down
11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@
"index.js"
],
"reporters": [
[
"./",
{
"logLevel": "INFO",
"showInternalStackTraces": false
}
]
["./", {
"logLevel": "INFO",
"showInternalStackTraces": false
}]
],
"testRegex": "(test|src)\\/.+\\.(test|spec)\\.jsx?$"
},
Expand Down
13 changes: 1 addition & 12 deletions src/LineWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,6 @@ const REG_DIFFERENCE = /^\s*Difference:/;
const MDASH = '\u2014';
const CIRCLE = '●';

const FAIL_TEXT = 'FAIL';
const PASS_TEXT = 'PASS';

const FAIL = chalk.supportsColor ?
chalk`{reset.inverse.bold.red ${FAIL_TEXT} }` :
` ${FAIL_TEXT} `;

const PASS = chalk.supportsColor ?
chalk`{reset.inverse.bold.green ${PASS_TEXT} }` :
` ${PASS_TEXT} `;

class LineWriter {
constructor (logger, root) {
this.counter = 0;
Expand Down Expand Up @@ -298,7 +287,7 @@ class LineWriter {
}

suite (isFail, dir, base) {
const label = isFail ? FAIL : PASS;
const label = isFail ? chalk`{reset.inverse.bold.red FAIL }` : chalk`{reset.inverse.bold.green PASS }`;

this.comment(chalk`${label} {grey ${this.getPathRelativeToRoot(dir)}${path.sep}}{bold ${base}}`);
}
Expand Down
78 changes: 63 additions & 15 deletions src/TapReporter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable id-match, class-methods-use-this, no-console */
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const LoggerTemporal = require('./loggers/LoggerTemporal');
Expand All @@ -10,13 +11,19 @@ const STATUS_PENDING = 'pending';

const sShouldFail = Symbol('shouldFail');

// eslint-disable-next-line no-process-env
const isCI = () => Boolean(process.env.CI);

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

this.globalConfig = globalConfig;
this.options = options;
this.setOptions(options);

const logger = new LoggerTemporal({
logLevel: this.options.logLevel,
stream: this.createOutputStream()
});

this[sShouldFail] = false;
this.writer = new LineWriter(logger, globalConfig.rootDir);

Expand All @@ -25,6 +32,43 @@ class TapReporter {
this.onRunStartOptions = {};
}

setOptions (options) {
if (!options.showProgress || isCI() || options.filePath) {
options.showProgress = false;
} else {
options.showProgress = true;
}

if (!options.logLevel) {
options.logLevel = 'INFO';
}

if (options.filePath) {
chalk.level = 0;
}

options.showHeader = options.showHeader === undefined ? true : Boolean(options.showHeader);

this.options = options;
}

writingToFile () {
return Boolean(this.options.filePath);
}

createOutputStream () {
const {filePath} = this.options;

if (filePath) {
const {rootDir} = this.globalConfig;
const filename = path.isAbsolute(filePath) ? filePath : path.join(rootDir, filePath);

return fs.createWriteStream(filename);
} else {
return process.stdout;
}
}

pathRelativeToRoot (filePath) {
return path.relative(this.globalConfig.rootDir, filePath);
}
Expand Down Expand Up @@ -69,7 +113,9 @@ class TapReporter {
onRunStart (results, options) {
this.onRunStartOptions = options;

this.writer.start(results.numTotalTestSuites);
if (this.options.showHeader) {
this.writer.start(results.numTotalTestSuites);
}
}

onTestResult (test, testResult, aggregatedResults) {
Expand Down Expand Up @@ -98,20 +144,22 @@ class TapReporter {
});
}

this.writer.logger.temporary();
if (this.options.showProgress) {
this.writer.logger.temporary();

this.writer.blank();
this.writer.aggregatedResults(aggregatedResults);
this.writer.blank();
this.writer.aggregatedResults(aggregatedResults);

const {estimatedTime} = this.onRunStartOptions;
const {estimatedTime} = this.onRunStartOptions;

if (estimatedTime) {
const startTime = aggregatedResults.startTime;
const percentage = (Date.now() - startTime) / 1e3 / estimatedTime / 3;
if (estimatedTime) {
const startTime = aggregatedResults.startTime;
const percentage = (Date.now() - startTime) / 1e3 / estimatedTime / 3;

if (percentage <= 1) {
this.writer.blank();
this.writer.timeProgressBar(percentage);
if (percentage <= 1) {
this.writer.blank();
this.writer.timeProgressBar(percentage);
}
}
}

Expand Down

0 comments on commit 8dbb637

Please sign in to comment.