Skip to content

Commit 209c0b2

Browse files
authored
feat(jest): create Jest danger plugin (#1)
1 parent ee2dd96 commit 209c0b2

File tree

10 files changed

+4509
-7
lines changed

10 files changed

+4509
-7
lines changed

README.md

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,81 @@
11
# danger-plugin-jest
22

3-
TODO
3+
[![Build Status](https://travis-ci.org/macklinu/danger-plugin-jest.svg?branch=master)](https://travis-ci.org/macklinu/danger-plugin-jest)
4+
[![npm version](https://badge.fury.io/js/danger-plugin-jest.svg)](https://badge.fury.io/js/danger-plugin-jest)
5+
[![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)
6+
7+
> [Danger](https://github.com/danger/danger-js) plugin for Jest
8+
9+
## Usage
10+
11+
### Setup Jest
12+
13+
This Danger plugin relies on modifying your Jest configuration.
14+
15+
Install [jest-json-reporter](https://github.com/Vall3y/jest-json-reporter):
16+
17+
```sh
18+
yarn add jest-json-reporter --dev
19+
```
20+
21+
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`):
22+
23+
```json
24+
{
25+
"jest": {
26+
"testResultsProcessor": "jest-json-reporter"
27+
},
28+
"jestJsonReporter": {
29+
"outputFile": "tests/results.json"
30+
}
31+
}
32+
```
33+
34+
> You may also want to add the JSON output file to your `.gitignore`, since it doesn't need to be checked into source control.
35+
36+
### Setup Danger
37+
38+
Install this Danger plugin:
39+
40+
```sh
41+
yarn add danger-plugin-jest --dev
42+
```
43+
44+
If you set `jestJsonReporter.outputFile` in your `package.json`, make sure that `testResultsJsonPath` matches that path:
45+
46+
```js
47+
// dangerfile.js
48+
import path from 'path'
49+
import jest from 'danger-plugin-jest'
50+
51+
jest({
52+
testResultsJsonPath: path.resolve(__dirname, 'tests/results.json'),
53+
})
54+
```
55+
56+
If you _did not_ change the `jestJsonReporter.outputFile` path in your `package.json`, you can just do the following:
57+
58+
```js
59+
// dangerfile.js
60+
import jest from 'danger-plugin-jest'
61+
62+
jest()
63+
```
64+
65+
See [`src/index.ts`](https://github.com/macklinu/danger-plugin-jest/blob/master/src/index.ts) for more details.
66+
67+
## Changelog
68+
69+
See the GitHub [release history](https://github.com/macklinu/danger-plugin-jest/releases).
70+
71+
## Development
72+
73+
Install [Yarn](https://yarnpkg.com/en/), and install the dependencies - `yarn install`.
74+
75+
Run the [Jest](https://facebook.github.io/jest/) test suite with `yarn test`.
76+
77+
This project uses [semantic-release](https://github.com/semantic-release/semantic-release) for automated NPM package publishing.
78+
79+
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).
80+
81+
:heart:

__tests__/index.test.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"commit": "git-cz",
1313
"commitmsg": "validate-commit-msg",
1414
"build": "tsc -p tsconfig.json",
15-
"lint": "tslint '{__tests__/**/*.test.ts,src/**/*.ts}' -c tslint.json -p tsconfig.json",
15+
"lint": "tslint 'src/**/*.ts' -c tslint.json -p tsconfig.json",
1616
"test": "jest",
1717
"docs": "typedoc --theme minimal --out docs src/index.ts",
1818
"docs:serve": "npm run docs && serve docs",
@@ -72,7 +72,7 @@
7272
}
7373
},
7474
"lint-staged": {
75-
"{__tests__/**/*.test.ts,src/**/*.ts}": [
75+
"src/**/*.ts": [
7676
"tslint -c tslint.json -p tsconfig.json --fix",
7777
"git add"
7878
]

src/TestFailureFormatter.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* A simple formatter that formats only test failures for use by Danger.
3+
*/
4+
export class TestFailureFormatter {
5+
constructor(private results: any) {}
6+
7+
public format(): string {
8+
return this.results.testResults
9+
.filter((r: any) => r.failureMessage)
10+
.map((r: any) => r.failureMessage)
11+
.join('\n')
12+
}
13+
}

0 commit comments

Comments
 (0)