100% Test Coverage?
When you write unit tests, do you try to achieve 100% test coverage or are you content with anything above 80% test coverage?
Most of the time, I feel that trying to achieve 100% test coverage is a waste of time. The return of investment starts to diminish once the coverage goes above 80%.
However, what happens today make me reconsider the benefits of trying to achieve 100% test coverage.
This was the original code:
The coverage tool complained that "line 11" was not covered.
My initial reaction was, "does it matter?". Anyhow, I tried to write a test to create a scenario to trigger the default assignment in "line 11". After running the new test, I was surprised to find out that "isFullyCheckedBy" return 'true' when "data" is undefined. The expected correct behavior should be 'false'!!!
And then, I find out why. It was because nothing stops the code running all the way to "line 19" when "entries" is an empty array.
This is my solution to fix the bug:
I added a check at "line 13" to exit the function to return 'false' when "entries" is an empty array.
What do you think? Do you agree at the fix? Are you convinced that it is important to achieve 100% test coverage?
Most of the time, I feel that trying to achieve 100% test coverage is a waste of time. The return of investment starts to diminish once the coverage goes above 80%.
However, what happens today make me reconsider the benefits of trying to achieve 100% test coverage.
This was the original code:
10 function isFullyCheckedBy (type, data) {
11 const { entries = [] } = data
12
13 for (const entry of entries) {
14 const { results = {} } = entry
15 if (results[type] !== true) {
16 return false
17 }
18 }
19 return true
20 }
21
The coverage tool complained that "line 11" was not covered.
My initial reaction was, "does it matter?". Anyhow, I tried to write a test to create a scenario to trigger the default assignment in "line 11". After running the new test, I was surprised to find out that "isFullyCheckedBy" return 'true' when "data" is undefined. The expected correct behavior should be 'false'!!!
And then, I find out why. It was because nothing stops the code running all the way to "line 19" when "entries" is an empty array.
This is my solution to fix the bug:
10 function isFullyCheckedBy (type, data) {
11 const { entries = [] } = data
12
13 if (entries.length == 0) return false
14
15 for (const entry of entries) {
16 const { results = {} } = entry
17 if (results[type] !== true) {
18 return false
19 }
20 }
21 return true
22 }
23
I added a check at "line 13" to exit the function to return 'false' when "entries" is an empty array.
What do you think? Do you agree at the fix? Are you convinced that it is important to achieve 100% test coverage?
Comments
Post a Comment