From 93bdfe275adf8f15bd323b84d8878b8ee5b308da Mon Sep 17 00:00:00 2001 From: Dan Homola Date: Sat, 31 Aug 2019 16:41:01 +0200 Subject: [PATCH] feat: add support for reporterAPI usage This makes the reporter config the default and deprecates testResultsProcessor. Fixes #32 BREAKING CHANGE: Reporter API is the default, testResultsProcessor is deprecated. --- README.md | 48 +++++++++++++++++++++++----- examples/scripts/jestTrxProcessor.js | 4 +-- src/index.ts | 36 +++++++++++---------- src/testResultsProcessor.ts | 23 +++++++++++++ src/trx-generator.ts | 10 +++++- 5 files changed, 94 insertions(+), 27 deletions(-) create mode 100644 src/testResultsProcessor.ts diff --git a/README.md b/README.md index d3a5712..b7cacdc 100644 --- a/README.md +++ b/README.md @@ -8,24 +8,56 @@ This package is used to export a TRX file from [Jest](https://facebook.github.io/jest/) test runs to be used in [Visual Studio](https://www.visualstudio.com/) and [Visual Studio Team Services](https://www.visualstudio.com/vsts-test/). -## Usage +## Migration from 0.x + +Version 1.0.0 was rewritten to use reporters API. Either use that (see [Usage](#usage)), or update the require path in your configuration (see [Usage as testResultsProcessor](#usage-as-test-results-processor)). -First install the package to your project from npm as a `devDependency`: +## Installation ``` -npm install jest-trx-results-processor --save-dev +yarn add --dev jest-trx-results-processor ``` -or if you prefer [`yarn`](https://yarnpkg.com/): +## Usage + +In your jest config add the following entry: +```json +{ + "reporters": ["default", "jest-trx-results-processor"] +} ``` -yarn add --dev jest-trx-results-processor + +You can also pass additional arguments: + +```json +{ + "reporters": [ + "default", + [ + "jest-trx-results-processor", + { + "outputFile": "relative/path/to/resulting.trx", // defaults to "test-results.trx" + "defaultUserName": "user name to use if automatic detection fails" // defaults to "anonymous" + } + ] + ] +} ``` -Then create a `jestTrxProcessor.js` file somewhere in your project (for this example I'll assume the `scripts` folder). +Then run jest as usual. + +## Usage as testResultsProcessor + +_Notice: this method of use will be removed in the next major version. PLease use the reporters API described above._ + +Create a `jestTrxProcessor.js` file somewhere in your project (for this example I'll assume the `scripts` folder). There you can configure the processor, as Jest does not allow you to pass custom parameters to the results processor: ```js +// for jest-trx-results-processor >= 1.0.0 +var builder = require("jest-trx-results-processor/dist/testResultsProcessor"); // only this has changed since v 0.x +// for jest-trx-results-processor < 1.0.0 var builder = require("jest-trx-results-processor"); var processor = builder({ @@ -36,7 +68,7 @@ var processor = builder({ module.exports = processor; ``` -Finally point Jest to your results processor in the `package.json`: +Finally, point Jest to your results processor in the `package.json`: ```json { @@ -56,7 +88,7 @@ Minimal working configuration can be seen in the [examples folder](https://githu ## Acknowledgements -This tool is heavily inspired by [karma-trx-reporter](https://github.com/hatchteam/karma-trx-reporter). +This tool is heavily inspired by [karma-trx-reporter](https://github.com/hatchteam/karma-trx-reporter) and [jest-junit](https://github.com/jest-community/jest-junit). ## License diff --git a/examples/scripts/jestTrxProcessor.js b/examples/scripts/jestTrxProcessor.js index 6ed3258..3c3cf82 100644 --- a/examples/scripts/jestTrxProcessor.js +++ b/examples/scripts/jestTrxProcessor.js @@ -1,7 +1,7 @@ -var builder = require('jest-trx-results-processor'); +var builder = require("jest-trx-results-processor/dist/testResultsProcessor"); var processor = builder({ - outputFile: 'relative/path/to/resulting.trx' // this defaults to "test-results.trx" + outputFile: "relative/path/to/resulting.trx", // this defaults to "test-results.trx" }); module.exports = processor; diff --git a/src/index.ts b/src/index.ts index b7eefab..598e3e1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,23 +1,27 @@ import { AggregatedResult } from "@jest/test-result"; +import { Config } from "@jest/types"; import { writeFileSync } from "fs"; import { generateTrx, IOptions } from "./trx-generator"; -const processor = ( - options: IOptions = { - outputFile: "test-results.trx", - defaultUserName: "anonymous", - }, -) => (testRunResult: AggregatedResult): AggregatedResult => { - process.stdout.write("Generating TRX file..."); +class TrxReporter { + constructor( + _: Config.GlobalConfig, + private options: IOptions = { + outputFile: "test-results.trx", + defaultUserName: "anonymous", + }, + ) {} - const trx = generateTrx(testRunResult, options); + public onRunComplete = ( + _: any, + aggregatedResults: AggregatedResult, + ): Promise | void => { + const trx = generateTrx(aggregatedResults, this.options); - writeFileSync(options.outputFile, trx, { encoding: "utf8" }); - process.stdout.write("DONE\n"); - process.stdout.write(`TRX file output to '${options.outputFile}'\n`); + writeFileSync(this.options.outputFile, trx, { encoding: "utf8" }); + process.stdout.write("DONE\n"); + process.stdout.write(`TRX file output to '${this.options.outputFile}'\n`); + } +} - // Return the input testRunResult to allow for chaining other result processors - return testRunResult; -}; - -export = processor; +module.exports = TrxReporter; diff --git a/src/testResultsProcessor.ts b/src/testResultsProcessor.ts new file mode 100644 index 0000000..b7eefab --- /dev/null +++ b/src/testResultsProcessor.ts @@ -0,0 +1,23 @@ +import { AggregatedResult } from "@jest/test-result"; +import { writeFileSync } from "fs"; +import { generateTrx, IOptions } from "./trx-generator"; + +const processor = ( + options: IOptions = { + outputFile: "test-results.trx", + defaultUserName: "anonymous", + }, +) => (testRunResult: AggregatedResult): AggregatedResult => { + process.stdout.write("Generating TRX file..."); + + const trx = generateTrx(testRunResult, options); + + writeFileSync(options.outputFile, trx, { encoding: "utf8" }); + process.stdout.write("DONE\n"); + process.stdout.write(`TRX file output to '${options.outputFile}'\n`); + + // Return the input testRunResult to allow for chaining other result processors + return testRunResult; +}; + +export = processor; diff --git a/src/trx-generator.ts b/src/trx-generator.ts index e34ceb1..4408da3 100644 --- a/src/trx-generator.ts +++ b/src/trx-generator.ts @@ -90,9 +90,17 @@ const renderResultSummary = ( parentNode: XMLElement, testRunResult: AggregatedResult, ) => { + // workaround for https://github.com/facebook/jest/issues/6924 + const anyTestFailures = !( + testRunResult.numFailedTests === 0 && + testRunResult.numRuntimeErrorTestSuites === 0 + ); + + const isSuccess = !(anyTestFailures || testRunResult.snapshot.failure); + const summary = parentNode .ele("ResultSummary") - .att("outcome", testRunResult.success ? "Passed" : "Failed"); + .att("outcome", isSuccess ? "Passed" : "Failed"); summary .ele("Counters")