DEV Community

AmasiaNalbandian
AmasiaNalbandian

Posted on

How to test your code without testing your patience.

this week I attempted to add testing features to my CLI application which I had created earlier this semester.

I wish I could say this was easy - as I thought it might be.. However, I learned a few things that I will share here.

The hurdles:

  1. Implement a package that worked with modules (haha - none!);
  2. code blocks with absolutely no returns
  3. Asynchronous functions
  4. Continuous function calls (Russian dolls)

rule #1:
To write tests for your code you should have a plan. I can't stress this enough. Due to the nature of this being a lab, I had a deadline - and as you guessed it, it's tonight. However, your planning should start before you start writing your code. You should begin questioning yourself how you want your code to be organized so that it is easier to test. This will really depend on the purpose of your code, and might differ each time. All I know is I was asking myself a few questions such as:

  • Why did I use modules?
  • Why do I have a function that does something and then enters into another function?
  • Why are my functions asynchronous - when it does not need to be?
  • Is my code structured as best as possible? (No - I wanted to tear the whole thing apart)

rule#2:

Research a test package you want to work with. Know its limitations. Look up the various perks of it. I learned that Jasmine is a bit behind in terms of other test packages such as Jest. I tried to install Jest first, but because i had modules in my code, I was not able to configure this. I read about Jasmine being fine with this, but that was not true either. I ended up changing one file for the testing, and eventually did that for all. I could have used Jest after all, but I was just about done half my tests by then.

rule#3

It's difficult to test asynchronous function calls. I had to use a timeout, but I wasn't too sure this was the best approach either. Timing needs to be taken into consideration into the planning phase of your code. Does your code need to wait for data before being able to continue?

Results:

In the end, I feel like my tests are still not the best. I rewrote some functions, and tried to add some returns. The structure is clearly poor as there are function calls within function calls creating a blackhole of function calls. As I mentioned before, I wish I had planned that I would be implementing testing into the application. I know I would have paid much more attention to the way I had written my code - which would have helped tremendously.

In addition, writing the tests did reveal some discrepancies, which I did fix - so my code is a little better than what I started with.

Learning how to write tests was actually really easy. I had tried to write a few in WEB422 - and remember the syntax is usually not too difficult. I think as you write more tests, and READ more tests you see new things that you can do. Maybe I will find a way to work with asynchronous functions in my tests one day - but for now, my plan is to rewrite the application before I add any more tests.

Top comments (0)