Put Your Code to the Test

You’ve probably run into this problem: some nerd on your team made some function that found it’s way into production. It works well and all, functioning as a function should function… except when it doesn’t. Sometimes, this function has some unintended side effects that throw off a part of your application. No problem says some clever nerd on your team, she’s got a quick fix that should nip this thing in the bud. She squishes this bug then hands off the product to QA who tests it to make sure said side effect no longer occurs. All is well again.

And then… a few weeks later, a user discovers another bug related to the fix for the function clever dev #2 made. Another nerd joins the dogpile and puts in a fix for the first fix. The cycle continues for months until your team is on fix #28 for this function with a rich and profane history of git commits. Fuck that function.

I will admit, I’ve been/am in this position: writing code on a team, leaving testing to QA and my own, incredibly biased logic and console logs. I’m a bit embarrassed that I just began unit testing and still have quite a ways to go, but I now see my reluctance was really due to my fear of the unknown.

While I’m using Chutzpah and Jasmine on Visual Studios for Angular apps, let’s make a quick test suite using mocha-chai and node:

First, get a project with js folder and file app.js –> && test folder with file test.js.

npm init -y to initialize your package.json file

npm install mocha chai -g to install mocha and chai globally (mocha is the testing framework while chai is the assertion library or the thing that allows you to actually test your functions)

Ok, we’re on a goddamned role here. Let’s write some sweet ass code:

In your app.js file let’s make a testable function:

var testFunction = function(str){
return parseInt(str);

if(typeof exports !== 'undefined') {
exports.testFunction = testFunction;

That conditional statement below is how we will export our browser functions if we are running mocha in our node environment.

var test = require('../js/app.js')
var expect = require('chai').expect;

describe('testFunction', function(){
it('should return a number when given a string', function(){

First, we require our js file and then require chai’s `expect` method. The rest of our test suite is pretty self-explanatory: we describe what we expect our function to do and then assert the output of our function given a certain input. In this case we say that the string value ‘3’ should return the integer 3 after getting run through our sweet (haha get it) test.

To actually run this test, open up your terminal, navigate to the the test folder and mocha test.js. You should see this in your terminal:

Screen Shot 2017-09-07 at 9.33.50 PM

Now, that was a pretty simple function, but that’s kinda the point. Testing really only works if you’re writing code the way you should be with functions that do one thing rather than many. The added benefit of using testing is that it forces you to really think about what your functions should be doing and hold them to their word, creating nice documentation for future nerds and hopefully saving you from the 23rd iteration of that pesky production bug.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s