Javascript unit-testing, and other recipes

Alex Szabó
4 min readJan 19, 2021

It was 5:40 pm, 20 minutes ‘till my usual hours are off. I wanted to finish implementing that module rewrite for running the automated translation synchronization before the weekend hits. It was Friday, so I planned to stop working on time, but you know how it goes with home-office… it’s easier to stay in front of the computer than unplug and pack away.

I was only starting to write tests, and my module wasn’t even finished implementation-wise. The tests required some rewriting… My code kind of looked like Jest/Jasmine style tests, every step its own section, with some signaling before-after, executed sequentially. But I needed to do some refactoring to expose these steps as functions for testing… So I started throwing out the complex (albeit nicely working) structures, to dumb it back down to a collection of functions.

I, like most people, feel bad for throwing out perfectly good code, especially if it’s something clever…

but it has to be done!

It’s a random screenshot of some error message. But your eyes are so used to ignoring shitty stock-photos like this, you don’t even notice this is here. Or you do, and you also read the caption text, in that case: hi!

Code that you wrote shouldn’t be kept only because you wrote it, especially if it’s something clever.

It was 5:58, I took a deep breath, stood up, walked into the kitchen… I poured a glass of water and noticed the recently washed and dried frying pan next to me, that’s when I realized, I still have some potatoes at home, and I am getting kind of hungry…

Amazing pan-fried potato frittata

Ingredients for 1 person:

The key point with this recipe is to chop everything to reasonably small bits so they cook better, even in a pan, where hot contact surface is rare.

1. Start with the potatoes, those require the most cooking. Clean, peel, and cut in half. Now that you have half potatoes that rest stable against your chopping-board, go ahead and slice them up as thin as you’d slice onions. Then do the crosscuts too to get small blocks of potato.

2. Next, do the same with your onion, chop it finely.

3. With some butter or olive oil and some salt, start cooking your potatoes and onion, give it a good 10 minutes to cook properly, while occasionally stirring.

4. While your potatoes are cooking in onion-sweat, you can dice up the tomatoes and the sausage. But wait with adding them.

I went back to my computer. With a pair of fresh eyes, I realized why it would be trouble to test these functions… Most of them do interact with APIs or files, as the whole module is basically a list of steps for some automated tasks. My tests would basically consist of almost the same amount of lines, mimicking every line of the original code, and basically freezing the implementation in place. Not adding much safety, and tugging down on flexibility by having to update every test’s expectation whenever I touch the implementation.

Does it really make sense to write unit tests for this code?

I looked up to the clock: 18:11 — screw this, I’m going back to my frittata.

5. Once your onions are browning, and your potatoes are getting cooked, you can add the tomatoes and meat of choice. Make sure to crank up the stove to catch up with the heat with the cold tomatoes.

6. I usually add flavoring at this time. In addition to the salt we’ve added earlier, the tomatoes can handle a little bit more, also pepper, garlic, and some fresh chilies if you like it spicy.

7. Give your pan another 5–10 minutes to cook again after the heat-reset of the tomatoes.

8. Mix the cream cheese with the egg and add to the mix while stirring. After this, let the concoction cook for another 2–3 minutes for the egg, but otherwise, you should be ready.

For serving, I like to add some fresh basil leaves or some more hot sauce.

I grew up in a normal, Eastern-European country-side house, with a traditional family, with the mother being the main chef in the house. But my mom never prepared this recipe for me. It’s probably because I just invented this from the ingredients I found in my kitchen.

Enjoy!

(This was an original post on LinkedIn, but who the heck reads Linkedin posts anyway. We go there for positive reaffirmation on our horrible nature, and to see if any hotties checked our profile in the past 24 hours)

--

--