JavaScript TDD with Jasmine: the FizzBuzz kata (screencast)

Written on

Recently I have been writing a lot of JavaScript, and have enjoyed using Test Driven Development (TDD) while doing so. Last month I gave an hour-long live-coding demo at the Trondheim XP & Agile meetup and showed using TDD on JavaScript in action. One of the demo's I did was implementing FizzBuzz. The JavaScript testing framework I used was Jasmine BDD.

I regularly practice coding kata's - simple programming problems that you can solve again and again. The reason I do this is to learn keyboard shortcuts, learn new unit testing frameworks, and just in general, keep my programming skills sharp and fresh.

I recorded a screencast of me using Jasmine and TDD to solve the FizzBuzz game in JavaScript. I recorded the screencast using Screenr and it is available to view here:

Here is the screencast embedded via YouTube:


Nice. I have a "puristic" comment though: In TDD we should write just enough code to make the test pass. You are violating that principle here. E.g. in the first test for GetResult(3) you should have written just return "fizz"; in the production code. (Instead you write ..if (isMultipleOfThree(number)) return "fizz"; ..which is too much logic to make the test pass). Nice anyway ! Good music.

Lars-Erik Roald

I did that for the purpose of speed. When I practice this kata normally, I change two other things as well as what you point out: first, I have an array of multiples of 3/5/15 and loop over them in the tests and assert, instead of just checking one number - giving my tests broader reach. Secondly, I usually add a final refactoring at the end and create a method called IsMultipleOfThreeAndFive() for eye candy. But doing all of that would have knocked it over the 5 minute barrier ;-)