Skip to content

Commit 66018e5

Browse files
authored
feat(tolerance): add -t, --tolerance option (#16)
1 parent b69f172 commit 66018e5

File tree

6 files changed

+117
-16
lines changed

6 files changed

+117
-16
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@ $ jest-it-up --help
6363
Usage: jest-it-up [options]
6464

6565
Options:
66-
-c, --config <path> path to a Jest config file (default: 'jest.config.js')
67-
-m, --margin <margin> minimum threshold increase (default: 0)
68-
-i, --interactive ask for confirmation before applying changes
69-
-s, --silent do not output messages
70-
-d, --dry-run process but do not change files
71-
-v, --version output the version number
72-
-h, --help display help for command
66+
-c, --config <path> path to a Jest config file (default: 'jest.config.js')
67+
-m, --margin <margin> minimum threshold increase (default: 0)
68+
-t, --tolerance <tolerance> threshold difference from actual coverage
69+
-i, --interactive ask for confirmation before applying changes
70+
-s, --silent do not output messages
71+
-d, --dry-run process but do not change files
72+
-v, --version output the version number
73+
-h, --help display help for command
7374
```

bin/jest-it-up

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ const jestItUp = require('../lib')
77
program
88
.option('-c, --config <path>', 'path to a Jest config file', 'jest.config.js')
99
.option('-m, --margin <margin>', 'minimum threshold increase', parseFloat, 0)
10+
.option(
11+
'-t, --tolerance <tolerance>',
12+
'threshold difference from actual coverage',
13+
parseFloat,
14+
0,
15+
)
1016
.option('-i, --interactive', 'ask for confirmation before applying changes')
1117
.option('-s, --silent', 'do not output messages')
1218
.option('-d, --dry-run', 'process but do not change files')

lib/__tests__/getNewThresholds.test.js

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ it('returns new thresholds if coverages are higher', () => {
2020
statements: { pct: 50 },
2121
}
2222
const margin = 0
23+
const tolerance = 0
2324

24-
const newThresholds = getNewThresholds(thresholds, coverages, margin)
25+
const newThresholds = getNewThresholds(
26+
thresholds,
27+
coverages,
28+
margin,
29+
tolerance,
30+
)
2531

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

49-
const newThresholds = getNewThresholds(thresholds, coverages, margin)
56+
const newThresholds = getNewThresholds(
57+
thresholds,
58+
coverages,
59+
margin,
60+
tolerance,
61+
)
5062

5163
expect(newThresholds).toStrictEqual({
5264
branches: { diff: 70, next: 80, prev: 10 },
5365
functions: { diff: 50, next: 70, prev: 20 },
5466
})
5567
})
68+
69+
it('should return new thresholds if coverage - tolerance is higher than the current threshold', () => {
70+
const thresholds = {
71+
branches: 10,
72+
functions: 20,
73+
lines: 30,
74+
statements: 40,
75+
}
76+
const coverages = {
77+
branches: { pct: 80 },
78+
functions: { pct: 70 },
79+
lines: { pct: 60 },
80+
statements: { pct: 50 },
81+
}
82+
const margin = 0
83+
const tolerance = 10
84+
85+
const newThresholds = getNewThresholds(
86+
thresholds,
87+
coverages,
88+
margin,
89+
tolerance,
90+
)
91+
92+
expect(newThresholds).toStrictEqual({
93+
branches: { diff: 60, next: 70, prev: 10 },
94+
functions: { diff: 40, next: 60, prev: 20 },
95+
lines: { diff: 20, next: 50, prev: 30 },
96+
statements: { diff: 0, next: 40, prev: 40 },
97+
})
98+
})
99+
100+
it('should not return new thresholds if coverage - tolerance is lower than the current threshold', () => {
101+
const thresholds = {
102+
branches: 10,
103+
functions: 20,
104+
lines: 30,
105+
statements: 40,
106+
}
107+
const coverages = {
108+
branches: { pct: 80 },
109+
functions: { pct: 70 },
110+
lines: { pct: 60 },
111+
statements: { pct: 50 },
112+
}
113+
const margin = 0
114+
const tolerance = 100
115+
116+
const newThresholds = getNewThresholds(
117+
thresholds,
118+
coverages,
119+
margin,
120+
tolerance,
121+
)
122+
123+
expect(newThresholds).toStrictEqual({})
124+
})

lib/__tests__/index.test.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ it('runs with with default options', async () => {
3434
expect(getData).toHaveBeenCalledWith('/workingDir/jest.config.js')
3535

3636
expect(getNewThresholds).toHaveBeenCalledTimes(1)
37-
expect(getNewThresholds).toHaveBeenCalledWith('thresholds', 'coverages', 0)
37+
expect(getNewThresholds).toHaveBeenCalledWith('thresholds', 'coverages', 0, 0)
3838

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

7373
expect(getNewThresholds).toHaveBeenCalledTimes(1)
74-
expect(getNewThresholds).toHaveBeenCalledWith('thresholds', 'coverages', 10)
74+
expect(getNewThresholds).toHaveBeenCalledWith(
75+
'thresholds',
76+
'coverages',
77+
10,
78+
0,
79+
)
7580
})
7681

7782
it.each([true, false])(
@@ -124,3 +129,15 @@ it('runs with custom config', async () => {
124129
expect(getData).toHaveBeenCalledTimes(1)
125130
expect(getData).toHaveBeenCalledWith('/workingDir/customDir/jest.config.js')
126131
})
132+
133+
it('runs with custom tolerance', async () => {
134+
await jestItUp({ tolerance: 10 })
135+
136+
expect(getNewThresholds).toHaveBeenCalledTimes(1)
137+
expect(getNewThresholds).toHaveBeenCalledWith(
138+
'thresholds',
139+
'coverages',
140+
0,
141+
10,
142+
)
143+
})

lib/getNewThresholds.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
const getNewThresholds = (thresholds, coverages, margin) =>
1+
const getNewThresholds = (thresholds, coverages, margin, tolerance) =>
22
Object.entries(thresholds).reduce((acc, [type, threshold]) => {
33
const { pct: coverage } = coverages[type]
44

5+
const desiredCoverage = coverage - tolerance
6+
57
// Only update threshold if new coverage is higher than
68
// current threshold + margin
7-
if (coverage >= threshold + margin) {
9+
if (desiredCoverage >= threshold + margin) {
810
acc[type] = {
911
prev: threshold,
10-
next: coverage,
11-
diff: +(coverage - threshold).toFixed(2),
12+
next: desiredCoverage,
13+
diff: +(desiredCoverage - threshold).toFixed(2),
1214
}
1315
}
1416

lib/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ module.exports = async ({
1414
dryRun = false,
1515
interactive = false,
1616
margin = 0,
17+
tolerance = 0,
1718
silent = false,
1819
} = {}) => {
1920
const configPath = path.resolve(process.cwd(), config)
2021

2122
const { thresholds, coverages } = await getData(configPath)
22-
const newThresholds = getNewThresholds(thresholds, coverages, margin)
23+
const newThresholds = getNewThresholds(
24+
thresholds,
25+
coverages,
26+
margin,
27+
tolerance,
28+
)
2329
const { changes, data } = getChanges(configPath, newThresholds)
2430

2531
if (!silent) {

0 commit comments

Comments
 (0)