Test-driven development (TDD) is an AGILE methodology that flips the development lifecycle by ensuring that tests are written first, before the code is implemented, and that tests drive the development (and are not just used as a validation tool).
The tenets of TDD are simple:
- Code is written only when there is a failing test that requires the code to pass
- The bare minimum amount of code is written to ensure that the test passes
- Duplication is removed at every step
- Once all tests are passing, the next failing test is added for the next required functionality
These simple rules ensure that:
- Your code develops organically, and that every line of code written is purposeful
- Your code remains highly modular, cohesive, and reusable (as you need to be able to test it)
- You provide a comprehensive array of tests to prevent future breakages and bugs
- The tests also act as specification, and thus documentation, for future needs and changes
Tools for javascript TDD
Installing and using Karma
- install node.js
- run to install Karma globally and also save as devDependencies in package.json:
npm install -g karma --save-dev
- run to install Karma command line interface globally:
npm install -g karma-cli --save-dev
- run:
npm install karma-jasmine --save-dev
- run:
npm install karma-chrome-launcher --save-dev
- run to generate initial Karma configuration file:
karma init
- run to start Karma:
karma start