How do I test this function?

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:

  1. 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.
  2. Publish code on forum as text, not as images, not everyone is blessed with sharp eyesight and images are poor for accessibility.
  3. 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:

  1. Easier to notice possible improvements (for example no need for cd as you can use Yarn flag for that)
  2. Easier to understand (you will not really need that comment)
  3. 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:

  1. 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.
  2. I will remember this one for any future posts.
  3. This sounds way more logical indeed, I will implement this and see how it works.