Skip to content
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The action assumes jest configuration and jest module already present in the wor

Sample workflow for running this action

```
```yaml
name: Node.js CI

on: pull_request
Expand All @@ -70,7 +70,8 @@ jobs:
id: testCoverage
uses: anuraag016/Jest-Coverage-Diff@master
with:
fullCoverageDiff: false // defaults to false, if made true whole coverage report is commented with the diff
fullCoverageDiff: false # defaults to false, if made true whole coverage report is commented with the diff
directory: ./src # defaults to '.'
runCommand: "npx jest --collectCoverageFrom='[\"src/**/*.{js,jsx,ts,tsx}\"]' --coverage --collectCoverage=true --coverageDirectory='./' --coverageReporters='json-summary' --forceExit --detectOpenHandles test/.*test.*"
delta: 0.5
```
104 changes: 65 additions & 39 deletions __tests__/DiffChecker.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DiffChecker } from '../src/DiffChecker'
import {DiffChecker} from '../src/DiffChecker'

describe('DiffChecker', () => {
const mock100Coverage = {
Expand Down Expand Up @@ -77,9 +77,9 @@ describe('DiffChecker', () => {
' :x: | ~~file4~~ | ~~100~~ | ~~100~~ | ~~100~~ | ~~100~~'
])
})
describe("testing checkIfTestCoverageFallsBelowDelta", () => {
describe("respects total_delta for total and delta for other files", () => {
it("returns true because delta diff is too high, even if total_delta is okay", () => {
describe('testing checkIfTestCoverageFallsBelowDelta', () => {
describe('respects total_delta for total and delta for other files', () => {
it('returns true because delta diff is too high, even if total_delta is okay', () => {
const codeCoverageOld = {
total: mock100CoverageFile,
file1: mock100CoverageFile
Expand All @@ -89,10 +89,13 @@ describe('DiffChecker', () => {
file1: mock98CoverageFile
}
const diffChecker = new DiffChecker(codeCoverageNew, codeCoverageOld)
const isTestCoverageFallsBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(1, 50)
expect(isTestCoverageFallsBelowDelta).toBeTruthy();
const isTestCoverageFallsBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(
1,
50
)
expect(isTestCoverageFallsBelowDelta).toBeTruthy()
})
it("returns true because total_delta diff is too high, even if delta is okay", () => {
it('returns true because total_delta diff is too high, even if delta is okay', () => {
const codeCoverageOld = {
total: mock100CoverageFile,
file1: mock100CoverageFile
Expand All @@ -102,10 +105,13 @@ describe('DiffChecker', () => {
file1: mock98CoverageFile
}
const diffChecker = new DiffChecker(codeCoverageNew, codeCoverageOld)
const isTestCoverageFallsBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(50, 1)
expect(isTestCoverageFallsBelowDelta).toBeTruthy();
const isTestCoverageFallsBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(
50,
1
)
expect(isTestCoverageFallsBelowDelta).toBeTruthy()
})
it("returns true if delta diff is too high - total_delta is not defined", () => {
it('returns true if delta diff is too high - total_delta is not defined', () => {
const codeCoverageOld = {
total: mock100CoverageFile,
file1: mock100CoverageFile
Expand All @@ -115,10 +121,13 @@ describe('DiffChecker', () => {
file1: mock98CoverageFile
}
const diffChecker = new DiffChecker(codeCoverageNew, codeCoverageOld)
const isTestCoverageFallsBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(1, null)
expect(isTestCoverageFallsBelowDelta).toBeTruthy();
const isTestCoverageFallsBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(
1,
null
)
expect(isTestCoverageFallsBelowDelta).toBeTruthy()
})
it("returns false if total_delta and delta are okay", () => {
it('returns false if total_delta and delta are okay', () => {
const codeCoverageOld = {
total: mock100CoverageFile,
file1: mock100CoverageFile
Expand All @@ -128,10 +137,13 @@ describe('DiffChecker', () => {
file1: mock98CoverageFile
}
const diffChecker = new DiffChecker(codeCoverageNew, codeCoverageOld)
const isTestCoverageFallsBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(50, 50)
expect(isTestCoverageFallsBelowDelta).toBeFalsy();
const isTestCoverageFallsBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(
50,
50
)
expect(isTestCoverageFallsBelowDelta).toBeFalsy()
})
it("returns false if delta is okay - total_delta is not defined", () => {
it('returns false if delta is okay - total_delta is not defined', () => {
const codeCoverageOld = {
total: mock100CoverageFile,
file1: mock100CoverageFile
Expand All @@ -141,54 +153,68 @@ describe('DiffChecker', () => {
file1: mock98CoverageFile
}
const diffChecker = new DiffChecker(codeCoverageNew, codeCoverageOld)
const isTestCoverageFallsBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(50, null)
expect(isTestCoverageFallsBelowDelta).toBeFalsy();
const isTestCoverageFallsBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(
50,
null
)
expect(isTestCoverageFallsBelowDelta).toBeFalsy()
})
})
it("detects that total coverage dropped below total_delta", () => {
it('detects that total coverage dropped below total_delta', () => {
const codeCoverageOld = {
total: mock100CoverageFile,
total: mock100CoverageFile
}
const codeCoverageNew = {
total: mock98CoverageFile,
total: mock98CoverageFile
}
const diffChecker = new DiffChecker(codeCoverageNew, codeCoverageOld)
const isTestCoverageFallsBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(2, 1)
expect(isTestCoverageFallsBelowDelta).toBeTruthy();
const isTestCoverageFallsBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(
2,
1
)
expect(isTestCoverageFallsBelowDelta).toBeTruthy()
})
it("detects that total coverage did not drop below total_delta", () => {
it('detects that total coverage did not drop below total_delta', () => {
const codeCoverageOld = {
total: mock100CoverageFile,
total: mock100CoverageFile
}
const codeCoverageNew = {
total: mock98CoverageFile,
total: mock98CoverageFile
}
const diffChecker = new DiffChecker(codeCoverageNew, codeCoverageOld)
const isTestCoverageFallsBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(1, 5)
expect(isTestCoverageFallsBelowDelta).toBeFalsy();
const isTestCoverageFallsBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(
1,
5
)
expect(isTestCoverageFallsBelowDelta).toBeFalsy()
})
it("detects that total coverage dropped below delta", () => {
it('detects that total coverage dropped below delta', () => {
const codeCoverageOld = {
total: mock100CoverageFile,
total: mock100CoverageFile
}
const codeCoverageNew = {
total: mock98CoverageFile,
total: mock98CoverageFile
}
const diffChecker = new DiffChecker(codeCoverageNew, codeCoverageOld)
const isTestCoverageFallsBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(1, null)
expect(isTestCoverageFallsBelowDelta).toBeTruthy();
const isTestCoverageFallsBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(
1,
null
)
expect(isTestCoverageFallsBelowDelta).toBeTruthy()
})
it("detects that total coverage did not drop below delta", () => {
it('detects that total coverage did not drop below delta', () => {
const codeCoverageOld = {
total: mock100CoverageFile,
total: mock100CoverageFile
}
const codeCoverageNew = {
total: mock98CoverageFile,
total: mock98CoverageFile
}
const diffChecker = new DiffChecker(codeCoverageNew, codeCoverageOld)
const isTestCoverageFallsBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(2, null)
expect(isTestCoverageFallsBelowDelta).toBeFalsy();
const isTestCoverageFallsBelowDelta = diffChecker.checkIfTestCoverageFallsBelowDelta(
2,
null
)
expect(isTestCoverageFallsBelowDelta).toBeFalsy()
})

})
})
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ inputs:
fullCoverageDiff:
description: 'get the full coverage with diff or only the diff'
default: false
directory:
description: 'custom working directory'
default: '.'
runCommand:
description: 'custom command to get json-summary'
default: 'npx jest --coverage --coverageReporters="json-summary" --coverageDirectory="./"'
Expand Down
21 changes: 12 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2030,6 +2030,7 @@ function run() {
const commitSha = github.context.sha;
const githubToken = core.getInput('accessToken');
const fullCoverage = JSON.parse(core.getInput('fullCoverageDiff'));
const directory = core.getInput('directory');
const commandToRun = core.getInput('runCommand');
const commandAfterSwitch = core.getInput('afterSwitchCommand');
const delta = Number(core.getInput('delta'));
Expand All @@ -2046,17 +2047,19 @@ function run() {
totalDelta = Number(rawTotalDelta);
}
let commentId = null;
child_process_1.execSync(commandToRun);
const codeCoverageNew = (JSON.parse(fs_1.default.readFileSync('coverage-summary.json').toString()));
child_process_1.execSync('/usr/bin/git fetch');
child_process_1.execSync('/usr/bin/git stash');
child_process_1.execSync(`/usr/bin/git checkout --progress --force ${branchNameBase}`);
child_process_1.execSync(commandToRun, { cwd: directory });
const codeCoverageNew = (JSON.parse(fs_1.default.readFileSync(`${directory}/coverage-summary.json`).toString()));
child_process_1.execSync('/usr/bin/git fetch', { cwd: directory });
child_process_1.execSync('/usr/bin/git stash', { cwd: directory });
child_process_1.execSync(`/usr/bin/git checkout --progress --force ${branchNameBase}`, {
cwd: directory
});
if (commandAfterSwitch) {
child_process_1.execSync(commandAfterSwitch);
child_process_1.execSync(commandAfterSwitch, { cwd: directory });
}
child_process_1.execSync(commandToRun);
const codeCoverageOld = (JSON.parse(fs_1.default.readFileSync('coverage-summary.json').toString()));
const currentDirectory = child_process_1.execSync('pwd')
child_process_1.execSync(commandToRun, { cwd: directory });
const codeCoverageOld = (JSON.parse(fs_1.default.readFileSync(`${directory}/coverage-summary.json`).toString()));
const currentDirectory = child_process_1.execSync('pwd', { cwd: directory })
.toString()
.trim();
const diffChecker = new DiffChecker_1.DiffChecker(codeCoverageNew, codeCoverageOld);
Expand Down
Loading