Skip to content

Commit

Permalink
feat: add support for reporterAPI usage
Browse files Browse the repository at this point in the history
This makes the reporter config the default and deprecates testResultsProcessor.

Fixes #32

BREAKING CHANGE: Reporter API is the default, testResultsProcessor is deprecated.
  • Loading branch information
no23reason committed Aug 31, 2019
1 parent 22d8c11 commit 93bdfe2
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 27 deletions.
48 changes: 40 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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
{
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions examples/scripts/jestTrxProcessor.js
Original file line number Diff line number Diff line change
@@ -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;
36 changes: 20 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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> | 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;
23 changes: 23 additions & 0 deletions src/testResultsProcessor.ts
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 9 additions & 1 deletion src/trx-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 93bdfe2

Please sign in to comment.