-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
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 |
There was a problem hiding this 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
app/models/Mortgage.ts
Outdated
@@ -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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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;
}
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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!
coverageThreshold: { | ||
global: { | ||
branches: 100, | ||
functions: 100, | ||
lines: 100, | ||
}, | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙌
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.