Skip to content

Commit

Permalink
feat(tolerance): add -t, --tolerance option (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
JCorpe26 authored Mar 26, 2023
1 parent b69f172 commit 66018e5
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 16 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ $ jest-it-up --help
Usage: jest-it-up [options]

Options:
-c, --config <path> path to a Jest config file (default: 'jest.config.js')
-m, --margin <margin> minimum threshold increase (default: 0)
-i, --interactive ask for confirmation before applying changes
-s, --silent do not output messages
-d, --dry-run process but do not change files
-v, --version output the version number
-h, --help display help for command
-c, --config <path> path to a Jest config file (default: 'jest.config.js')
-m, --margin <margin> minimum threshold increase (default: 0)
-t, --tolerance <tolerance> threshold difference from actual coverage
-i, --interactive ask for confirmation before applying changes
-s, --silent do not output messages
-d, --dry-run process but do not change files
-v, --version output the version number
-h, --help display help for command
```
6 changes: 6 additions & 0 deletions bin/jest-it-up
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ const jestItUp = require('../lib')
program
.option('-c, --config <path>', 'path to a Jest config file', 'jest.config.js')
.option('-m, --margin <margin>', 'minimum threshold increase', parseFloat, 0)
.option(
'-t, --tolerance <tolerance>',
'threshold difference from actual coverage',
parseFloat,
0,
)
.option('-i, --interactive', 'ask for confirmation before applying changes')
.option('-s, --silent', 'do not output messages')
.option('-d, --dry-run', 'process but do not change files')
Expand Down
73 changes: 71 additions & 2 deletions lib/__tests__/getNewThresholds.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ it('returns new thresholds if coverages are higher', () => {
statements: { pct: 50 },
}
const margin = 0
const tolerance = 0

const newThresholds = getNewThresholds(thresholds, coverages, margin)
const newThresholds = getNewThresholds(
thresholds,
coverages,
margin,
tolerance,
)

expect(newThresholds).toStrictEqual({
branches: { diff: 70, next: 80, prev: 10 },
Expand All @@ -45,11 +51,74 @@ it('only returns new thresholds if coverages are above the margin', () => {
statements: { pct: 50 },
}
const margin = 50
const tolerance = 0

const newThresholds = getNewThresholds(thresholds, coverages, margin)
const newThresholds = getNewThresholds(
thresholds,
coverages,
margin,
tolerance,
)

expect(newThresholds).toStrictEqual({
branches: { diff: 70, next: 80, prev: 10 },
functions: { diff: 50, next: 70, prev: 20 },
})
})

it('should return new thresholds if coverage - tolerance is higher than the current threshold', () => {
const thresholds = {
branches: 10,
functions: 20,
lines: 30,
statements: 40,
}
const coverages = {
branches: { pct: 80 },
functions: { pct: 70 },
lines: { pct: 60 },
statements: { pct: 50 },
}
const margin = 0
const tolerance = 10

const newThresholds = getNewThresholds(
thresholds,
coverages,
margin,
tolerance,
)

expect(newThresholds).toStrictEqual({
branches: { diff: 60, next: 70, prev: 10 },
functions: { diff: 40, next: 60, prev: 20 },
lines: { diff: 20, next: 50, prev: 30 },
statements: { diff: 0, next: 40, prev: 40 },
})
})

it('should not return new thresholds if coverage - tolerance is lower than the current threshold', () => {
const thresholds = {
branches: 10,
functions: 20,
lines: 30,
statements: 40,
}
const coverages = {
branches: { pct: 80 },
functions: { pct: 70 },
lines: { pct: 60 },
statements: { pct: 50 },
}
const margin = 0
const tolerance = 100

const newThresholds = getNewThresholds(
thresholds,
coverages,
margin,
tolerance,
)

expect(newThresholds).toStrictEqual({})
})
21 changes: 19 additions & 2 deletions lib/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ it('runs with with default options', async () => {
expect(getData).toHaveBeenCalledWith('/workingDir/jest.config.js')

expect(getNewThresholds).toHaveBeenCalledTimes(1)
expect(getNewThresholds).toHaveBeenCalledWith('thresholds', 'coverages', 0)
expect(getNewThresholds).toHaveBeenCalledWith('thresholds', 'coverages', 0, 0)

expect(getChanges).toHaveBeenCalledTimes(1)
expect(getChanges).toHaveBeenCalledWith(
Expand Down Expand Up @@ -71,7 +71,12 @@ it('runs with custom margin', async () => {
await jestItUp({ margin: 10 })

expect(getNewThresholds).toHaveBeenCalledTimes(1)
expect(getNewThresholds).toHaveBeenCalledWith('thresholds', 'coverages', 10)
expect(getNewThresholds).toHaveBeenCalledWith(
'thresholds',
'coverages',
10,
0,
)
})

it.each([true, false])(
Expand Down Expand Up @@ -124,3 +129,15 @@ it('runs with custom config', async () => {
expect(getData).toHaveBeenCalledTimes(1)
expect(getData).toHaveBeenCalledWith('/workingDir/customDir/jest.config.js')
})

it('runs with custom tolerance', async () => {
await jestItUp({ tolerance: 10 })

expect(getNewThresholds).toHaveBeenCalledTimes(1)
expect(getNewThresholds).toHaveBeenCalledWith(
'thresholds',
'coverages',
0,
10,
)
})
10 changes: 6 additions & 4 deletions lib/getNewThresholds.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const getNewThresholds = (thresholds, coverages, margin) =>
const getNewThresholds = (thresholds, coverages, margin, tolerance) =>
Object.entries(thresholds).reduce((acc, [type, threshold]) => {
const { pct: coverage } = coverages[type]

const desiredCoverage = coverage - tolerance

// Only update threshold if new coverage is higher than
// current threshold + margin
if (coverage >= threshold + margin) {
if (desiredCoverage >= threshold + margin) {
acc[type] = {
prev: threshold,
next: coverage,
diff: +(coverage - threshold).toFixed(2),
next: desiredCoverage,
diff: +(desiredCoverage - threshold).toFixed(2),
}
}

Expand Down
8 changes: 7 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ module.exports = async ({
dryRun = false,
interactive = false,
margin = 0,
tolerance = 0,
silent = false,
} = {}) => {
const configPath = path.resolve(process.cwd(), config)

const { thresholds, coverages } = await getData(configPath)
const newThresholds = getNewThresholds(thresholds, coverages, margin)
const newThresholds = getNewThresholds(
thresholds,
coverages,
margin,
tolerance,
)
const { changes, data } = getChanges(configPath, newThresholds)

if (!silent) {
Expand Down

0 comments on commit 66018e5

Please sign in to comment.