As mentioned in my previous blog post, our JavaScript developers have had to learn new skills with the introduction of a more mature approach to JavaScript development. One of these skills we have had to get to grip with is Test Driven Development (TDD), a process that requires a deep understanding of both the overall structure of an application and the software used as an aid to the process.

How do we use TDD?

TDD relies on developers understanding the scope of the feature they are developing and how that feature impacts the application as a whole. Development cycles should be very short with continuous integration testing taking place on the full application to spot bugs as part of the development process.

Good unit tests are useful for three very different but equally important aspects of development:

  • A design aid – tests are written prior to development to aid in the design / development process
  • Feature documentation – tests should document all the features of an application, giving full test coverage
  • Quality assurance / continuous delivery – failing tests should halt the delivery of a product so no bugs are introduced into the application.

Initially, a developer will write a failing test for the feature they are adding, they then develop the solution that fixes that test. No other pre-existing tests should be caused to fail as part of the development process.

The code then needs to be refactored to the point where it is modular and easy to understand.  This process is very important as it allows the developer to create very good, easy to understand solutions, delivered with confidence that no bugs have been added.

The language used in writing a test should be clear and descriptive of what the test is for, as this then allows the test to be used as an efficient bug documentation tool.  Developers can then more efficiently address the bug and make the required changes to fix the problem.

Why do we use TDD?

The TDD process is one that fits in with the Agile working methods our development team use. As well as that we also find it brings a number of benefits to the development team and the applications they are creating, including:

  • Encouraging developers to think about their code
  • Inspiring confidence in the product
  • Reducing the density of bugs
  • Encouraging the design of modular code that is improved with each design cycle
  • Reducing the complexity of code

How do you write a good test?

With JavaScript applications we use Karma (to run the tests) and Jasmine (to script the tests). The Karma test platform runs as a continuous integration server, detecting changes to the code base and rerunning the existing series of tests against it, so any breaking changes are flagged as part of the development process.

The Jasmine scripting language is an open source test framework that allows us to write feature based tests quickly and easily.

A good test should include the following things:

  • What are you testing and what should it do? A good description of the feature.
  • What is the expected result? The planned outcome, the value we actually test against. Our expected result should have no animosity.

Benefits of TDD to our clients

All this TDD is great for our development team, but our customers also benefit from the process as we believe it allows us to deliver confidently, efficiently and in an agile manner, with very few bugs being introduced as part of the development process.

All the AngularJS application we now create are delivered in this way, and we firmly believe our clients are seeing huge benefits, with application features now integrated within Agile User Stories, progress of features are now documented and visible to our clients, and feature delivery times, testing and reworking for bugs all being shorter.