Skip to content

Commit

Permalink
feat(precision): add -p, --precision option (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
saunders1989 authored Jan 24, 2024
1 parent e17d4ba commit 9238a06
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ npm install --save-dev jest-it-up

## Usage

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

Within `package.json`:

Expand Down Expand Up @@ -70,5 +70,6 @@ Options:
-s, --silent do not output messages
-d, --dry-run process but do not change files
-v, --version output the version number
-p, --precision number of threshold decimal places to persist
-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 @@ -16,6 +16,12 @@ program
.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')
.option(
'-p, --precision <digits>',
'number of threshold decimal places to persist',
parseInt,
2,
)
.version(version, '-v, --version')
.parse(process.argv)

Expand Down
76 changes: 74 additions & 2 deletions lib/__tests__/getNewThresholds.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ it('returns new thresholds if coverages are higher', () => {
}
const margin = 0
const tolerance = 0
const precision = 2

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

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

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

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

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

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

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 },
})
})

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

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

expect(newThresholds).toStrictEqual({})
})

it('should return new thresholds with a precision if set', () => {
const thresholds = {
branches: 10,
functions: 20,
lines: 30,
statements: 40,
}
const coverages = {
branches: { pct: 80.63 },
functions: { pct: 70.15 },
lines: { pct: 60.25 },
statements: { pct: 50.89 },
}
const margin = 0
const tolerance = 0
const precision = 1

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

expect(newThresholds).toStrictEqual({
branches: { diff: 70.6, next: 80.6, prev: 10 },
functions: { diff: 50.1, next: 70.1, prev: 20 },
lines: { diff: 30.2, next: 60.2, prev: 30 },
statements: { diff: 10.8, next: 50.8, prev: 40 },
})
})

it('should return new thresholds be a whole number if precision is set to 0', () => {
const thresholds = {
branches: 10,
functions: 20,
lines: 30,
statements: 40,
}
const coverages = {
branches: { pct: 80.6 },
functions: { pct: 70.1 },
lines: { pct: 60.25 },
statements: { pct: 50.89 },
}
const margin = 0
const tolerance = 0
const precision = 0

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

expect(newThresholds).toStrictEqual({
branches: { diff: 70, next: 80, prev: 10 },
functions: { diff: 50, next: 70, prev: 20 },
lines: { diff: 30, next: 60, prev: 30 },
statements: { diff: 10, next: 50, prev: 40 },
})
})
23 changes: 22 additions & 1 deletion lib/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ it('runs with with default options', async () => {
expect(getData).toHaveBeenCalledWith('/workingDir/jest.config.js')

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

expect(getChanges).toHaveBeenCalledTimes(1)
expect(getChanges).toHaveBeenCalledWith(
Expand Down Expand Up @@ -76,6 +82,7 @@ it('runs with custom margin', async () => {
'coverages',
10,
0,
2,
)
})

Expand Down Expand Up @@ -139,5 +146,19 @@ it('runs with custom tolerance', async () => {
'coverages',
0,
10,
2,
)
})

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

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

const desiredCoverage = coverage - tolerance
const factor = Math.pow(10, precision)
const nextCoverage = Math.trunc(desiredCoverage * factor) / factor

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

Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = async ({
margin = 0,
tolerance = 0,
silent = false,
precision = 2,
} = {}) => {
const configPath = path.resolve(process.cwd(), config)

Expand All @@ -25,6 +26,7 @@ module.exports = async ({
coverages,
margin,
tolerance,
precision,
)
const { changes, data } = getChanges(configPath, newThresholds)

Expand Down

0 comments on commit 9238a06

Please sign in to comment.