Skip to content

Commit

Permalink
feat: improve options and path resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Dec 13, 2017
1 parent ce917cc commit b55f490
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ You can add an optional configuration object:
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`.
- `filePath` - specifies a file to write the results. If not supplied it will use `process.stdout`.
- `noProgressReporting` - whether to not show intermediate test result summary while testing is in progress. In general, defaults to `false`. When writing to file or in CI environment, it is set to `true`.
- `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

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
],
"reporters": [
["./", {
"logLevel": "ERROR",
"logLevel": "INFO",
"showInternalStackTraces": false,
"filePath": "test.tap"
"filePath": "/Users/vadimdaleckis/lol.tap"
}]
],
"testRegex": "(test|src)\\/.+\\.(test|spec)\\.jsx?$"
Expand Down
55 changes: 37 additions & 18 deletions src/TapReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,14 @@ const isCI = () => Boolean(process.env.CI);

class TapReporter {
constructor (globalConfig = {}, options = {}) {
this.globalConfig = globalConfig;
this.setOptions(options);
const {logLevel, filePath} = this.options;

let stream = process.stdout;

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

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

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

Expand All @@ -41,14 +33,39 @@ class TapReporter {
}

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

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

if (isCI() || this.options.filePath) {
this.options.noProgressReporting = true;
if (options.filePath) {
chalk.level = 0;
}

if (!this.options.logLevel) {
this.options.logLevel = 'INFO';
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;
}
}

Expand Down Expand Up @@ -96,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 @@ -125,7 +144,7 @@ class TapReporter {
});
}

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

this.writer.blank();
Expand Down

0 comments on commit b55f490

Please sign in to comment.