I have been struggling for a few hours now, but I can’t find any way to properly test this function.
How would you go about testing this function?
I need this tested as my project requires 100% coverage.
Few things there:
- Targeting for 100% coverage is stupid, as it encourages writing dumb tests that checks only happy paths. ~90-95% is more reasonable target. It is better to test only meaningful functions deeply than doing shallow tests of everything.
- Publish code on forum as text, not as images, not everyone is blessed with sharp eyesight and images are poor for accessibility.
- You do not test this function as it does too many things:
- It fetches application environment
- It changes working directory (and never change it back)
- It spawns external process
- Split string (and does that needlessly eagerly, as you are interested only in 2nd element of list, so you need to split it into 3 parts, you do not care about rest of the lines)
- Base64 encode it
Split that function into manageable segments and you will find it:
- Easier to notice possible improvements (for example no need for
cd
as you can use Yarn flag for that)
- Easier to understand (you will not really need that comment)
- Easier to test (as you can test each of the functions independently from other parts)
9 Likes
Thank you very much for your answer. To address your points:
- I do not have any say in this as it is a graduation project and this is one of the requirements that was set, but I do agree as I have written mostly happy path tests.
- I will remember this one for any future posts.
- This sounds way more logical indeed, I will implement this and see how it works.