Writing testable Javascript


Writing testable Javascript

Writing testable Javascript

Earlier this week I was tasked with recreating the Etsy Shop using their RESTful API. We were asked to approach the problem by first pulling down the data, then using a router (backbone.js in this case) to place the info on the screen in its respective places. Anyway, I spent countless hours deliberating how all of this worked, but tonight I watched a short talk with Rebecca Murphey at Full Frontal and she explained to me (retrospectively) exactly what it is that I was doing.

During the process I ran into the godforsaken “Error: x [value] is undefined” but she introduced the idea of testing our js code before we run it. She stated some very simple yet true, “Facts of life” and they are as follows:

you will design your code

you will test your code

you will refactor your code

other people will use your code

there will be bugs

All of these things are true, but they’re much easier understood once you’ve spent hundreds of hours struggling through faulty code (especially as a newbie). Anyways, she presented several different platforms we can use as developers to test our code, so that 5 years from now we’re not afraid to update it in fear that it might “break”. Frameworks like Selenium, Grunt —mocha, Sinon.js, and chai.js are a great way to write tests for JS. Also, Unit Tests — help you write better code.

Unit tests basically allow you to run tests to make sure the given input matches it respective output. Much like a patchbay in the recording studio, Unit tests state explicitly that, “given input x, is the output y”. Running these tests are meant to make not only our lives better, but our colleagues and contributors as well.

In the code world, everyone has their preference (which if you’ve spent 5 minutes on stack overflow, you know what I mean) but what really matters is that our code works! Yes it is amazing to condense 400 lines of code into 175 but that will come with time and experience, which any master coder knows. However, these tests, separate subjective criticism from objective evaluation.

We all have guiding principles that we abide by when were creating anything creative, but sometimes you can learn something from another creative if you allow yourself the opportunity to take constructive criticism. With all this being said, I mentioned earlier that I was tasked with creating the Etsy shop from scratch just using their API. Well, I was introduced to setting up templates, Constructors, and Prototypes; and I can’t tell you how much shorter my code got from my previous attempt to “go it alone”.

There are websites out here that can explain all this, but there is nothing better than hearing a human speak (especially when you work in a computer world) about their techniques.

In all, you end up writing a 1:1 ratio as far as lines of code, but in the end, everything just work! Try testing your Javascript code yourself before you “refresh” and see how you like it!


0 Comments Leave a reply

    Leave a comment

    Your comment(click button to send)

    This is a unique website which will require a more modern browser to work!

    Please upgrade today!