Skip to content

Commit

Permalink
feat(jest): create Jest danger plugin (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
macklinu authored May 24, 2017
1 parent ee2dd96 commit 209c0b2
Show file tree
Hide file tree
Showing 10 changed files with 4,509 additions and 7 deletions.
80 changes: 79 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,81 @@
# danger-plugin-jest

TODO
[![Build Status](https://travis-ci.org/macklinu/danger-plugin-jest.svg?branch=master)](https://travis-ci.org/macklinu/danger-plugin-jest)
[![npm version](https://badge.fury.io/js/danger-plugin-jest.svg)](https://badge.fury.io/js/danger-plugin-jest)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)

> [Danger](https://github.com/danger/danger-js) plugin for Jest
## Usage

### Setup Jest

This Danger plugin relies on modifying your Jest configuration.

Install [jest-json-reporter](https://github.com/Vall3y/jest-json-reporter):

```sh
yarn add jest-json-reporter --dev
```

Modify your `package.json` to process test results with jest-json-reporter. You may optionally set the output path of the JSON test results using the `jestJsonReporter.outputFile` path (which otherwise defaults to `./test-results.json`):

```json
{
"jest": {
"testResultsProcessor": "jest-json-reporter"
},
"jestJsonReporter": {
"outputFile": "tests/results.json"
}
}
```

> You may also want to add the JSON output file to your `.gitignore`, since it doesn't need to be checked into source control.
### Setup Danger

Install this Danger plugin:

```sh
yarn add danger-plugin-jest --dev
```

If you set `jestJsonReporter.outputFile` in your `package.json`, make sure that `testResultsJsonPath` matches that path:

```js
// dangerfile.js
import path from 'path'
import jest from 'danger-plugin-jest'

jest({
testResultsJsonPath: path.resolve(__dirname, 'tests/results.json'),
})
```

If you _did not_ change the `jestJsonReporter.outputFile` path in your `package.json`, you can just do the following:

```js
// dangerfile.js
import jest from 'danger-plugin-jest'

jest()
```

See [`src/index.ts`](https://github.com/macklinu/danger-plugin-jest/blob/master/src/index.ts) for more details.

## Changelog

See the GitHub [release history](https://github.com/macklinu/danger-plugin-jest/releases).

## Development

Install [Yarn](https://yarnpkg.com/en/), and install the dependencies - `yarn install`.

Run the [Jest](https://facebook.github.io/jest/) test suite with `yarn test`.

This project uses [semantic-release](https://github.com/semantic-release/semantic-release) for automated NPM package publishing.

The main caveat: instead of running `git commit`, run `yarn commit` and follow the prompts to input a conventional changelog message via [commitizen](https://github.com/commitizen/cz-cli).

:heart:
1 change: 0 additions & 1 deletion __tests__/index.test.ts

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"commit": "git-cz",
"commitmsg": "validate-commit-msg",
"build": "tsc -p tsconfig.json",
"lint": "tslint '{__tests__/**/*.test.ts,src/**/*.ts}' -c tslint.json -p tsconfig.json",
"lint": "tslint 'src/**/*.ts' -c tslint.json -p tsconfig.json",
"test": "jest",
"docs": "typedoc --theme minimal --out docs src/index.ts",
"docs:serve": "npm run docs && serve docs",
Expand Down Expand Up @@ -72,7 +72,7 @@
}
},
"lint-staged": {
"{__tests__/**/*.test.ts,src/**/*.ts}": [
"src/**/*.ts": [
"tslint -c tslint.json -p tsconfig.json --fix",
"git add"
]
Expand Down
13 changes: 13 additions & 0 deletions src/TestFailureFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* A simple formatter that formats only test failures for use by Danger.
*/
export class TestFailureFormatter {
constructor(private results: any) {}

public format(): string {
return this.results.testResults
.filter((r: any) => r.failureMessage)
.map((r: any) => r.failureMessage)
.join('\n')
}
}
Loading

0 comments on commit 209c0b2

Please sign in to comment.