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

Unit test 100% coverage #72

Merged
merged 16 commits into from
Aug 28, 2024
Merged

Unit test 100% coverage #72

merged 16 commits into from
Aug 28, 2024

Conversation

gabrielegranello
Copy link
Collaborator

What does this PR do?

It expands the suite of unit tests following #71

How?

By checking which lines of code were not read in the jest report, I have added a few extra tests to explore those lines. In doing so, I caught a small bug in the Mortgage.ts class.
Furthermore, the jest.config.ts file is modified to force 100% of the code coverage while testing.

Copy link

vercel bot commented Aug 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
fairhold-dashboard ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 28, 2024 8:49am

@gabrielegranello
Copy link
Collaborator Author

This is interesting... When testing on my machine I get to 100%, but when testing on Github the percentage is lower. How can this be possible?

Screenshot 2024-08-26 at 14 15 11

@gabrielegranello
Copy link
Collaborator Author

gabrielegranello commented Aug 26, 2024

That drove me bananas I swear. Basically the node version on my local machine was 18.17.0; In the GitHub Actions, node version was 18. That meant for some obscure reason unknown to me that the line coverage was different. I have even made sure to delete the cache when running the test. I still can't explain myself why but now it works

@gabrielegranello gabrielegranello marked this pull request as ready for review August 26, 2024 12:49
@gabrielegranello gabrielegranello requested a review from a team August 26, 2024 12:49
Copy link
Contributor

@DafyddLlyr DafyddLlyr left a comment

Choose a reason for hiding this comment

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

Amazing, good result here! Great to see that we caught some issues this way as well.

Not exactly sure of the reason you were having issues with Node and coverage percentages. One good idea would be to set up a .nvmrc file in the root of the project which may allow us to ensure we have a standard version of Node locally when running the app? https://dev.to/kibumpng/consistency-and-efficiency-with-nvmrc-30mi

@@ -92,7 +92,7 @@ export class Mortgage {
];

for (let i = 0; i < this.termYears - 1; i++) {
if (i != this.termYears - 1) {
if (i != this.termYears - 2) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Magic number! 🪄 🎩 🐇

Maybe if (!isFinalYear) would be a suitable variable name we could use?

const isFinalYear = i == this.termYears - 2

Also, often positive conditions are easier to read - could we use if (isFinalYear) as a guard clause and un-nest this condition?

Copy link
Collaborator Author

@gabrielegranello gabrielegranello Aug 26, 2024

Choose a reason for hiding this comment

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

Thanks Daf, it makes a lot of sense. Were you thinking of something like this?

    const finalYear = this.termYears - 2;
    for (let i = 0; i < this.termYears - 1; i++) {
      if (i == finalYear) {
        yearlyPayment = remainingBalance;
      } else {
        yearlyPayment = this.monthlyPayment * MONTHS_PER_YEAR;
      }

Copy link
Contributor

@DafyddLlyr DafyddLlyr Aug 26, 2024

Choose a reason for hiding this comment

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

Here's a version using a guard clause pattern -

const isFinalYear = this.termYears - 2;
for (let i = 0; i < this.termYears - 1; i++) {
  if (i == isFinalYear) {
    yearlyPayment = remainingBalance;
    continue;
  }
  
  yearlyPayment = this.monthlyPayment * MONTHS_PER_YEAR;
}

Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue

Copy link
Contributor

Choose a reason for hiding this comment

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

P.S. GitHub (and lots of other markdown / code tools - even Notion) allow you to annotate code blocks with a language to get the nice syntax highlighting features 💅

Docs: https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks#syntax-highlighting

image

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for taking the time to spell it out @DafyddLlyr, really appreciated.
It was the first time I have come across this patter for loops, I have always used if/else. I can see why this structure is better. Also great tip for Typescript!

Comment on lines +47 to +53
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
},
},
Copy link
Contributor

Choose a reason for hiding this comment

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

🙌

@gabrielegranello gabrielegranello merged commit 345652b into main Aug 28, 2024
3 checks passed
@gabrielegranello gabrielegranello deleted the gg/unit-test-expansion branch August 28, 2024 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants