TDD in Javascript

Before starting build my Enigma simulator, I decided to use a Test-Driven Development (TDD) approach. Why? Because I believe that with TDD every programmer - including you - can produce more robust software, do easier refactoring, and meet the software specifications.

For those who never heard about TDD, it pretty much a different style of programming advocated by Kent Beck and David Astels, some really influential guys in the wild. Instead of writing your classes and methods and then testing it - either manually or with auxiliary scripts - to ensure it’s working, TDD states that we must write tests in the first place. TDD, can be resumed in three steps:

  1. Write the specifications of what you want in the format of an automated test and verify it fails
  2. Implement the functionality to make the test pass in the easiest possible way (as baby steps that are small but takes you there)
  3. Refactor the code to make it more simple, elegant, performatic, readable, reusable…

To apply TDD to my JavaScript code, I decided to use two libraries with names of drinks: mocha and chai. Let me explain them.

mocha is a test framework that runs on Node.js. It basically runs over your code looking for test functions, execute them in parallel, and output the passing and failing tests in a pretty output format. On the other hand, chai is an assertion library. It pretty much offers three functions for asserting inside tests: should, expect, and assert. You can find more information on these assertion styles in their docs.

After writing some code, that’s how I execute them:

$ mocha enigma.js

That’s it. With mocha and chai, I’m all set for writing JavaScript using TDD. What do you use in your programming environment for TDD? Please, let me know by sending me tweet to my twitter account @matheusvportela. You can also check all the tests I’ve written so far by following my GitHub repository.