Setting The Goal Posts Before Kicking The Ball: Making The Case For Test-Driven Development

Unit testing is now rightly accepted as an integral step of software development. Software reliability feeds directly into business sustainability for many companies: broken software can tarnish a business’ reputation, cause major disruption for customers, and cost a business a lot of money.

Particularly as component-based software has become standard, the impact of broken code can be widespread and not limited to only one feature. A high degree of test coverage for an application is very important. One approach to unit testing is Test-Driven Development.

The basic idea behind Test-Driven Development is that unit tests are written before a piece of code is written. Whilst being a simple concept in and of itself, its benefits are manifold.

Setting The Goal Posts At The Beginning

By writing the tests at the outset, the developer has stated the objectives before writing any code and these objectives must then be met, as opposed to using the outcome of the code to determine what the objectives are.

Devising Assertions

Without getting into the details of any of the tests, the assertions ensure that there is a clear focus for the developer on the code’s raison d’être and help to prevent drift from occurring in the code.

Assessing Overall Structures

With the approach of Test-Driven Development, writing unit tests at the outset facilitates clear thinking about the fundamental flow and structure of the code. Basic pieces of functionality are designed and constructed without getting into lower level details.

Identify Helper And Utility Functions Early On

Documentation

Recommended Testing Tools for JavaScript

  • expect(value).toEqual(expectedValue);
  • expect(value).toBeFalsy();
  • expect(value).toMatchObject(expectedValue);

Jest also provides a comprehensive mocking library which allows the developer to abstract functionality for testing. These include timer mocks, manual mocks, ES6 class mocks and bypassing module mocks.

A third great feature that is provided by Jest is Snapshot Testing. This checks to see if the outcome of a given test matches a previously saved outcome. If not, then the test will fail.

Another excellent testing tool for JavaScript is Mocha. This framework predates Jest and is also a test-runner, similar to Jest. It is a lot more configurable that Jest but it requires a greater effort to set up. It allows the developer to choose libraries to use for assertions, mocking and spying. For larger projects that require a lot of test customisation, Mocha may be the better choice.

There are also other widely used JavaScript testing tools that are specific to particular front-end libraries. These include:

Conclusion

Ultimately, it makes for good software and in turn, better business.

For these reasons, Test-Driven Development is a highly recommended approach for unit testing and software development in general.

JavaScript Enthusiast and Professional

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store