Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(precision): add -p, --precision option #20

Merged
merged 4 commits into from
Jan 24, 2024

Conversation

saunders1989
Copy link
Contributor

We have an issue where we want to bump the coverage but don't want to bump it by 2 decimal places. We want to bump it by a whole number. so if our margin is 1 and the increase is 1.65% we want to go from 40 -> 41 instead of 41.65.

I think this also addresses the issues been spoken about here - #17

Also please feel free to rip this apart and tell me if you'd like changes. it would be great to get this in :)

… can increase the coverage by a whole number
@rbardini rbardini added the enhancement New feature or request label Dec 26, 2023
@rbardini rbardini self-requested a review December 26, 2023 21:39
Copy link
Owner

@rbardini rbardini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @saunders1989! Left a couple comments.

Also, please remember to update the README with the new option. You can run jest-it-up --help and copy-paste the output.

bin/jest-it-up Outdated
Comment on lines 19 to 22
.option(
'-w, --whole-number',
'round coverage thresholds down to whole a whole number',
)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, but I think we could have a more generic solution:

Suggested change
.option(
'-w, --whole-number',
'round coverage thresholds down to whole a whole number',
)
.option('-p, --precision <digits>',
'number of threshold decimal places to persist',
parseInt,
2,
)

You can then do +desiredCoverage.toFixed(precision) in getNewThresholds, and pass --precision 0 if you want whole numbers. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of what you have proposed however i just found out that if the coverage is 30.6, and we want precision 0 it will then round up to 31. technically the coverage isnt at 31 so would fail i think

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, I forgot toFixed() rounds the number. In that case we would need something like this:

const factor = Math.pow(10, precision)
Math.trunc(desiredCoverage * factor) / factor

It may still round numbers due to floating point arithmetic though (e.g. 4.27 with precision 24.26), but I think that's acceptable, especially since it's opt-in. Alternatively, we could manipulate the number as a string, but that can get ugly pretty quickly.

@@ -9,7 +9,7 @@ const getNewThresholds = (thresholds, coverages, margin, tolerance) =>
if (desiredCoverage >= threshold + margin) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should calculate the rounded/truncated desiredCoverage value before this condition, otherwise we may end up updating the number to the same value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not quite following what you mean about calculating the number first. could you just explain in a bit more detail please

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may need to calculate the final desiredCoverage value (accounting for the truncation) before entering this if block.

Let's say desiredCoverage == 50.25, threshold == 50.2, margin == 0. Assuming we support custom precision, and it's 1 in this case, we actually don't need to update coverage thresholds, because the new value would be the same as the current one: 50.2.

So the math you do below must be done earlier, likely together with tolerance:

const desiredCoverage = Math.trunc((coverage - tolerance) * factor) / factor

In fact, we should probably replace the >= with > as well to avoid a 0 bump, but that can be left to a separate PR.

@rbardini rbardini linked an issue Dec 28, 2023 that may be closed by this pull request
@saunders1989
Copy link
Contributor Author

@rbardini Hey thanks for the speedy comments back. I've tried to implement what you've talked about. let me know what you'd like changing :)

@schinery
Copy link

Wondering if you'd had a chance to look at this @rbardini since the changes were made?

Would love to have this rounding stuff available in a couple of projects 🙏🏻

@rbardini
Copy link
Owner

Sorry for the delay, folks! LGTM, but index.js tests are failing 😕

@saunders1989 would you mind having a look, please? Some getNewThresholds assertions there are missing the new precision argument.

@saunders1989
Copy link
Contributor Author

@rbardini sorry about that. obviously didn't run all the tests locally 🤦. i have fixed and updated them so should be good now.

@codecov-commenter
Copy link

codecov-commenter commented Jan 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (e17d4ba) 100.00% compared to head (da2b740) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##            master       #20   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            7         7           
  Lines           69        71    +2     
=========================================
+ Hits            69        71    +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rbardini rbardini changed the title Round down to a whole number if the margin has been reached feat(precision): add -p, --precision option Jan 24, 2024
@rbardini
Copy link
Owner

Awesome, thanks @saunders1989!

@rbardini rbardini merged commit 9238a06 into rbardini:master Jan 24, 2024
3 checks passed
Copy link

🎉 This PR is included in version 3.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rounding?
4 participants