From b55f4901c7c7415d99dfde740405131d52932d73 Mon Sep 17 00:00:00 2001 From: Vadim Dalecky Date: Wed, 13 Dec 2017 11:02:33 +0000 Subject: [PATCH] feat: improve options and path resolution --- README.md | 5 +++-- package.json | 4 ++-- src/TapReporter.js | 55 +++++++++++++++++++++++++++++++--------------- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 4903208..448e54a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index ccbfda5..f91f736 100644 --- a/package.json +++ b/package.json @@ -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?$" diff --git a/src/TapReporter.js b/src/TapReporter.js index ac03300..d048785 100755 --- a/src/TapReporter.js +++ b/src/TapReporter.js @@ -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); @@ -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; } } @@ -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) { @@ -125,7 +144,7 @@ class TapReporter { }); } - if (!this.options.noProgressReporting) { + if (this.options.showProgress) { this.writer.logger.temporary(); this.writer.blank();