Skip to content

Commit 9238a06

Browse files
authored
feat(precision): add -p, --precision option (#20)
1 parent e17d4ba commit 9238a06

File tree

6 files changed

+118
-8
lines changed

6 files changed

+118
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ npm install --save-dev jest-it-up
2323

2424
## Usage
2525

26-
jest-it-up exposes a standalone CLI tool (see [options](#options)), but you most likelly want to use it in a post-test script.
26+
jest-it-up exposes a standalone CLI tool (see [options](#options)), but you most likely want to use it in a post-test script.
2727

2828
Within `package.json`:
2929

@@ -70,5 +70,6 @@ Options:
7070
-s, --silent do not output messages
7171
-d, --dry-run process but do not change files
7272
-v, --version output the version number
73+
-p, --precision number of threshold decimal places to persist
7374
-h, --help display help for command
7475
```

bin/jest-it-up

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ program
1616
.option('-i, --interactive', 'ask for confirmation before applying changes')
1717
.option('-s, --silent', 'do not output messages')
1818
.option('-d, --dry-run', 'process but do not change files')
19+
.option(
20+
'-p, --precision <digits>',
21+
'number of threshold decimal places to persist',
22+
parseInt,
23+
2,
24+
)
1925
.version(version, '-v, --version')
2026
.parse(process.argv)
2127

lib/__tests__/getNewThresholds.test.js

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ it('returns new thresholds if coverages are higher', () => {
2121
}
2222
const margin = 0
2323
const tolerance = 0
24+
const precision = 2
2425

2526
const newThresholds = getNewThresholds(
2627
thresholds,
2728
coverages,
2829
margin,
2930
tolerance,
31+
precision,
3032
)
3133

3234
expect(newThresholds).toStrictEqual({
@@ -52,17 +54,18 @@ it('only returns new thresholds if coverages are above the margin', () => {
5254
}
5355
const margin = 50
5456
const tolerance = 0
57+
const precision = 2
5558

5659
const newThresholds = getNewThresholds(
5760
thresholds,
5861
coverages,
5962
margin,
6063
tolerance,
64+
precision,
6165
)
6266

6367
expect(newThresholds).toStrictEqual({
6468
branches: { diff: 70, next: 80, prev: 10 },
65-
functions: { diff: 50, next: 70, prev: 20 },
6669
})
6770
})
6871

@@ -81,19 +84,20 @@ it('should return new thresholds if coverage - tolerance is higher than the curr
8184
}
8285
const margin = 0
8386
const tolerance = 10
87+
const precision = 2
8488

8589
const newThresholds = getNewThresholds(
8690
thresholds,
8791
coverages,
8892
margin,
8993
tolerance,
94+
precision,
9095
)
9196

9297
expect(newThresholds).toStrictEqual({
9398
branches: { diff: 60, next: 70, prev: 10 },
9499
functions: { diff: 40, next: 60, prev: 20 },
95100
lines: { diff: 20, next: 50, prev: 30 },
96-
statements: { diff: 0, next: 40, prev: 40 },
97101
})
98102
})
99103

@@ -112,13 +116,81 @@ it('should not return new thresholds if coverage - tolerance is lower than the c
112116
}
113117
const margin = 0
114118
const tolerance = 100
119+
const precision = 2
115120

116121
const newThresholds = getNewThresholds(
117122
thresholds,
118123
coverages,
119124
margin,
120125
tolerance,
126+
precision,
121127
)
122128

123129
expect(newThresholds).toStrictEqual({})
124130
})
131+
132+
it('should return new thresholds with a precision if set', () => {
133+
const thresholds = {
134+
branches: 10,
135+
functions: 20,
136+
lines: 30,
137+
statements: 40,
138+
}
139+
const coverages = {
140+
branches: { pct: 80.63 },
141+
functions: { pct: 70.15 },
142+
lines: { pct: 60.25 },
143+
statements: { pct: 50.89 },
144+
}
145+
const margin = 0
146+
const tolerance = 0
147+
const precision = 1
148+
149+
const newThresholds = getNewThresholds(
150+
thresholds,
151+
coverages,
152+
margin,
153+
tolerance,
154+
precision,
155+
)
156+
157+
expect(newThresholds).toStrictEqual({
158+
branches: { diff: 70.6, next: 80.6, prev: 10 },
159+
functions: { diff: 50.1, next: 70.1, prev: 20 },
160+
lines: { diff: 30.2, next: 60.2, prev: 30 },
161+
statements: { diff: 10.8, next: 50.8, prev: 40 },
162+
})
163+
})
164+
165+
it('should return new thresholds be a whole number if precision is set to 0', () => {
166+
const thresholds = {
167+
branches: 10,
168+
functions: 20,
169+
lines: 30,
170+
statements: 40,
171+
}
172+
const coverages = {
173+
branches: { pct: 80.6 },
174+
functions: { pct: 70.1 },
175+
lines: { pct: 60.25 },
176+
statements: { pct: 50.89 },
177+
}
178+
const margin = 0
179+
const tolerance = 0
180+
const precision = 0
181+
182+
const newThresholds = getNewThresholds(
183+
thresholds,
184+
coverages,
185+
margin,
186+
tolerance,
187+
precision,
188+
)
189+
190+
expect(newThresholds).toStrictEqual({
191+
branches: { diff: 70, next: 80, prev: 10 },
192+
functions: { diff: 50, next: 70, prev: 20 },
193+
lines: { diff: 30, next: 60, prev: 30 },
194+
statements: { diff: 10, next: 50, prev: 40 },
195+
})
196+
})

lib/__tests__/index.test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ 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, 0)
37+
expect(getNewThresholds).toHaveBeenCalledWith(
38+
'thresholds',
39+
'coverages',
40+
0,
41+
0,
42+
2,
43+
)
3844

3945
expect(getChanges).toHaveBeenCalledTimes(1)
4046
expect(getChanges).toHaveBeenCalledWith(
@@ -76,6 +82,7 @@ it('runs with custom margin', async () => {
7682
'coverages',
7783
10,
7884
0,
85+
2,
7986
)
8087
})
8188

@@ -139,5 +146,19 @@ it('runs with custom tolerance', async () => {
139146
'coverages',
140147
0,
141148
10,
149+
2,
150+
)
151+
})
152+
153+
it('runs with custom precision', async () => {
154+
await jestItUp({ precision: 0, tolerance: 10 })
155+
156+
expect(getNewThresholds).toHaveBeenCalledTimes(1)
157+
expect(getNewThresholds).toHaveBeenCalledWith(
158+
'thresholds',
159+
'coverages',
160+
0,
161+
10,
162+
0,
142163
)
143164
})

lib/getNewThresholds.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
const getNewThresholds = (thresholds, coverages, margin, tolerance) =>
1+
const getNewThresholds = (
2+
thresholds,
3+
coverages,
4+
margin,
5+
tolerance,
6+
precision,
7+
) =>
28
Object.entries(thresholds).reduce((acc, [type, threshold]) => {
39
const { pct: coverage } = coverages[type]
410

511
const desiredCoverage = coverage - tolerance
12+
const factor = Math.pow(10, precision)
13+
const nextCoverage = Math.trunc(desiredCoverage * factor) / factor
614

715
// Only update threshold if new coverage is higher than
816
// current threshold + margin
9-
if (desiredCoverage >= threshold + margin) {
17+
if (nextCoverage > threshold + margin) {
1018
acc[type] = {
1119
prev: threshold,
12-
next: desiredCoverage,
13-
diff: +(desiredCoverage - threshold).toFixed(2),
20+
next: nextCoverage,
21+
diff: +(nextCoverage - threshold).toFixed(2),
1422
}
1523
}
1624

lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = async ({
1616
margin = 0,
1717
tolerance = 0,
1818
silent = false,
19+
precision = 2,
1920
} = {}) => {
2021
const configPath = path.resolve(process.cwd(), config)
2122

@@ -25,6 +26,7 @@ module.exports = async ({
2526
coverages,
2627
margin,
2728
tolerance,
29+
precision,
2830
)
2931
const { changes, data } = getChanges(configPath, newThresholds)
3032

0 commit comments

Comments
 (0)