Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail on uncovered lines #83

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,30 @@ Then just run jest and it will test errors PRs!

```json
{

"reporters": ["default", "jest-github-reporter"]
}
```

### Additional Options

You may pass in additional options the the github reporter like so

```json
{
"reporters": [
"default",
["jest-github-reporter", { "failOnUncoveredLines": true }]
]
}
```

#### failOnUncoveredLines

If set to true, the reporter will comment on lines which were added in the pull request but
have no code coverage. If false, then uncovered lines will not fail the build.


```sh
jest file.js
```
Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"scripts": {
"build": "tsc -p tsconfig.json",
"lint": "eslint src --ext .ts",
"test": "jest",
"test:example": "jest --testLocationInResults example/",
"release": "auto shipit"
},
Expand All @@ -21,7 +22,8 @@
],
"dependencies": {
"@octokit/rest": "^16.28.7",
"create-check": "^0.6.1",
"create-check": "^0.6.36",
"istanbul-gh-pr-uncovered": "^1.0.0-beta.2",
"strip-ansi": "^6.0.0"
},
"devDependencies": {
Expand All @@ -39,9 +41,10 @@
"eslint-plugin-jsx-a11y": "6.2.3",
"eslint-plugin-prettier": "3.1.3",
"husky": "4.2.5",
"jest": "26.0.1",
"jest": "^26.1.0",
"lint-staged": "10.2.2",
"prettier": "2.0.5",
"ts-jest": "^26.1.2",
"typescript": "3.8.3"
},
"peerDependencies": {
Expand All @@ -58,6 +61,8 @@
},
"jest": {
"testLocationInResults": true,
"preset": "ts-jest",
"testEnvironment": "node",
"reporters": [
"default",
"./dist/index.js"
Expand All @@ -66,4 +71,4 @@
"auto": {
"extends": "hipstersmoothie"
}
}
}
69 changes: 63 additions & 6 deletions src/create-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { TestResult, AggregatedResult } from '@jest/reporters';

import path from 'path';
import stripAnsi from 'strip-ansi';
import getUncoveredPrFiles from 'istanbul-gh-pr-uncovered';
import createCheck, { Annotation } from 'create-check';
import groupSequences from './groupSequences';
import { GithubReporterConfig } from './types';

const APP_ID = 38833;
/**
Expand Down Expand Up @@ -54,6 +57,16 @@ interface Location {
line: number;
}

function getAppId() {
return process.env.JEST_APP_ID ? Number(process.env.JEST_APP_ID) : APP_ID;
}

function getPrivatekey() {
return process.env.JEST_PRIVATE_KEY || PRIVATE_KEY;
}



function createAnnotations(results: TestResult[]) {
const annotations: Annotation[] = [];

Expand Down Expand Up @@ -92,12 +105,56 @@ function createAnnotations(results: TestResult[]) {
return annotations;
}

export default (results: AggregatedResult) =>
createCheck({
async function createUncoveredLinesAnnotations(results: AggregatedResult) {
const annotations: Annotation[] = [];

const uncoveredPRFiles = await getUncoveredPrFiles({
coverageMap: results.coverageMap,
appId: getAppId(),
privateKey: getPrivatekey()
});

uncoveredPRFiles.forEach((ghPrFile: IstanbulGhPRUncovered.UncoveredFile) => {
const sequences = groupSequences(ghPrFile.lines);

// Group the lines together so that we don't post an annotation for lines that are adjacent to
// each other.
sequences.forEach(sequenceArray => {
const startLine = sequenceArray[0];
const endLine = sequenceArray[sequenceArray.length - 1];

annotations.push({
path: path.relative(process.cwd(), ghPrFile.filename),
start_line: startLine,
end_line: endLine,
annotation_level: 'failure',
message: startLine === endLine ? `This line is uncovered by tests.`
: `Lines ${startLine}-${endLine} are uncovered by tests.`
});

});
});

return annotations;
}



export default async (results: AggregatedResult, config: GithubReporterConfig) => {
const annotations: Annotation[] = createAnnotations(results.testResults);

if (config.failOnUncoveredLines) {
const uncoveredLinesAnnotations = await createUncoveredLinesAnnotations(results);
annotations.push(...uncoveredLinesAnnotations);
}

return createCheck({
tool: 'Jest',
name: 'Test',
annotations: createAnnotations(results.testResults),
errorCount: results.numFailedTests,
appId: process.env.JEST_APP_ID ? Number(process.env.JEST_APP_ID) : APP_ID,
privateKey: process.env.JEST_PRIVATE_KEY || PRIVATE_KEY
annotations,
errorCount: annotations.length,
appId: getAppId(),
privateKey: getPrivatekey()
});
}

16 changes: 16 additions & 0 deletions src/groupSequences.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import groupSequences from './groupSequences';

describe('groupSequences', () => {
it('should return an empty array when array is empty', () => {
expect(groupSequences([])).toEqual([]);
});
it('should return grouped sequences', () => {
const array = [1, 2, 3, 10, 11, 12, 13, 42];

expect(groupSequences(array)).toEqual([
[1, 2, 3],
joeyjiron06 marked this conversation as resolved.
Show resolved Hide resolved
[10, 11, 12, 13],
[42]
]);
});
})
20 changes: 20 additions & 0 deletions src/groupSequences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

/**
* @param {Number[]} numbers an array of sorted numbers
* @returns {Array[]} an array of array of numbers grouped by sequences
*/
export default (numbers: ReadonlyArray<number>) => {
if (numbers.length === 0) {
return [];
}

return numbers.reduce((array: number[][], number, index) => {
if (index === 0 || number !== numbers[index - 1] + 1) {
array.push([]);
}

array[array.length - 1].push(number);

return array;
}, []);
};
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
import { AggregatedResult, Test } from '@jest/reporters';

import createCheck from './create-check';
import { GithubReporterConfig } from './types';

class GitHubReporter {
// eslint-disable-next-line class-methods-use-this
private config: GithubReporterConfig;

constructor(globalConfig: jest.GlobalConfig, config: GithubReporterConfig) {
this.config = config;
}

async onRunComplete(
contexts: Set<Test['context']>,
testResult: AggregatedResult
) {
await createCheck(testResult);
await createCheck(testResult, this.config);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface GithubReporterConfig {
/**
* If set to true, the reporter will comment on lines which were added in the pull request but
* have no code coverage. If false, then uncovered lines will not fail the build.
*/
failOnUncoveredLines?: boolean;
};
17 changes: 13 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
{
"include": ["src/**/*", "test/**/*", "typings/**/*"],

"include": [
"src/**/*",
"test/**/*",
"typings/**/*"
],
"exclude": [
"node_modules",
"**/*.test.ts"
],
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["esnext"],
"lib": [
"esnext"
],
"outDir": "./dist",
"rootDir": "./src",
"declaration": true,
Expand All @@ -17,4 +26,4 @@
"resolveJsonModule": true,
"downlevelIteration": true
}
}
}