Javascript: Pure functions helped simplify unit testing for me.

Ken Aguilar
Caffeine and Testing
3 min readAug 25, 2016
https://unsplash.com/@tfaustin

I’m gonna start out by saying that I’m not an expert at functional programming. Functional programming is something that I have recently started learning. After learning just a few concepts of functional programming and applying it to my code my program already reads a lot better, and is a lot simpler with out any magic!

Before learning about pure functions I wondered how my state would change unexpectedly. When I run a program it produces an expected a result. However, the next time I run that program or I put in another input the result will totally be unexpected. Keeping track of the state with functions that mutate the state made it difficult to test, because of this I never tested my functions; and I just hoped for the best.

Let’s say I have a person object, and I have to add stuff to the favoriteFood list.

This would be my solution:

It does the work!

I call the function, put ‘ramen’ as an input and person object has now ramen in it as one of the favorite food. Great! It did what I wanted.

However, it is very hard to test this function once it’s mixed in with a bigger program. If there is an unexpected change I would have to do console.logs almost every line to check where the change is happening. It’s fun a couple of times hunting for that little bug but it becomes redundant and highly inefficient. It’s also a pain for other developers trying to help you out.

This is my attempt at testing that code:

Yay! But... As you can see I’m testing the person object not the favoriteFood() function. What if I have a bunch of operations acting on the person object, how can I keep track of all the changes? How can I make a test? I would have to keep track of all the mutations or changes to assert a result.

A couple of the fundamental concept of pure functions are:
1. A pure function must only operate on the input it is given and nothing else.
2. That function must return the output of that operation.
3. No side effect.

That’s it! It shouldn’t affect anything else, a pure function should not affect the person object.

With those concept, this is my amended function:

whoaa….

This solution is heavily influenced by Dan Abramov’s redux. I am using a reducer function, it takes in the state(person object) and an action. An action is an object containing an instruction and the payload.

The test!

Now, I only have to assert the output of the function in my test. I don’t have to account for other effects of other functions, I only have to test for the output of a single function. If all my functions are pure I wouldn’t have to worry about the state changing under my feet.

However, in order for my program to do something it has to have changes but instead of several functions changing the state. I need a central point where all changes happen. In redux it is called the store. The store is where all the output of the reducer function goes, but that subject is for another article!

This is my first article ever, I hope this was coherent; and I hope it will help developers who are still confused about unit testing. For any constructive criticism don’t hesitate to contact me. This will tremendously help me improve.

No state was harmed in the operation above…

--

--

Ken Aguilar
Caffeine and Testing

[Insert aspirational title here], Javascript/Typescript Nerd, Haskell Enthusiast