Embracing The Awesomeness Of Test Driven Development With Javascript

A question struck me badly recently while trying to figure out what TDD is for, why it's important to us as developers. and that would be

As we know, before an airplane is even bought for transport purposes, it must have gone through some rigorous test case by its manufacturers, and by this I mean months of production and testing, And just when it’s out there for the purpose of creation, it’s still being tested daily before usage.

As interesting as it is, I came across some amazing answers in Quora from experts and professionals, in all these answers a few caught my attention which I translate as.

  • To ensure the structural integrity of the control surfaces (nuts, bolts, screws).
  • To ensure that all the engine hoses and cables are where they should be and that nobody left a wrench or something in there after the last maintenance check.
  • To ensure that the fuel levels have sufficient fuel for flight plus additional fuel for unforeseen circumstances.
  • To validate the correctness of the control surfaces (e.g the left pedal makes the rudder deflect left, the left stick makes the left aileron go up, etc.).

It’s an amazing find and also embarrassing to ask. Why the safety checks before a plane takes off?

As crazy as it sound. Truth is, we talking of human lives here, and its of great important you put everything right, incheck and in control, cause no one wants to trust you with their lives when your system is not trusted.

Same applies to us as software engineers or developers, cause sometimes we might be dealing with serious confidentail informations and data and we would like to keep such an information secured and well protected from external access.

Yes we all know as a developer you have a good security sytem build in as to manage your user authentications and access to personal data.

But the questions remains, how sure are you of your security sysem for handling this authencations when you have unauthorized users in your system/app as it may be. In this relative, most times it might not be an Auth system but on severeal other parts of your app and how proven is the effectiveness of thier functionalities, if it's implemented properly and working right as it should, if there are errors, how is it been handled, is the logging system accurate enough to log all errors etc.

These in question, is were Test Driven Development comes in. What it is, and Why its important.

Introduction To Test-Driven Development

Test-Driven Development (TDD) is a software engineering practice that completely turns traditional development around. Its been around for years, and developers have been skeptical about it. Some have tried and failed, leading to the conclusion that TDD is not worth the effort it requires.

Some even think in theory, it is a good practice, but that there is never enough time to really use TDD. And others think that it is basically a waste of time, don’t get me wrong if I seem to alarm TDD is easy, trust me it's not, it can be frustrating sometimes when you are on a deadline project. As frankly as I can be and as clearly as it is, TDD is just but craftsmanship, the more you practice, the better you become and the faster you get at it.

Why Use Test-Driven Development

When using test-driven development methodology, It enforces you as a developer to focus on required feature implementations without adding unnecessary ones, so one can be able to create a minimalist yet functional and maintainable code, such as to reduce a good number of bugs already on the development stage before production, just like the airplane checks before the flight.

Benefits of using a test-driven development method

  • Modular programming: TDD enforces the understanding of basic principles on writing modular structured code.
  • Code maintenance: TDD supports and enforces an integral yet transparent process in writing software code and also reduces security risks during code reorganization, by this I mean. It allows us to refactor our code-base easily making the whole process much quicker and easier to perform.
  • Timely bug fixing: TDD enforces us, to start with writing tests before actual coding, this helps to eliminate bugs right from the start. This way, any issue can be detected and fixed way faster than usual.

Types of testing

Here are three of the most common types of automated tests:

  • Unit test: A single piece of code (usually an object or a function) is tested, isolated from other pieces
  • Integration test: Multiple pieces are tested together, for example, testing database access
  • Acceptance test (aka E2E test(End To End)): Automatic testing of the entire application, for example using a tool like Selenium to automatically run a browser.

Relatively to my Object Oriented Programming article with javascript found on this link here. I would like to walk us through testing each functionality of our Bookspy app and grasp an understanding of how TDD plays in real action. And for this article, I would be using Jest as our automated testing library. You can learn more about what it is and why it's loved above others here

Bookspy is an example of an online book library, where a user can register, subscribe to read books, buy books etc.

First, we are going to test our User class.

On this test, we went by instantiating a new user and saving him to the database, but on saving him to the database, we went by testing the function which saves the user to the database. To see what it returns if it was successful or not.

const saveUserToDataBase = jane.saveUser();expect(saveUserToDataBase).toMatch("Thank for registring with BookSpy");

We also went further, by testing the database if it's an array and if that array holds an object which is the new user we just created, also if the user holds the specific information needed.

expect(Array.isArray(DATABASE.userDB)).toEqual(true);
DATABASE.userDB.forEach(user => {
expect(typeof user).toEqual("object");
expect(Object.keys(user).sort()).toEqual(["email", "name", "password"]);
expect(typeof user.email).toEqual("string");
expect(typeof user.name).toEqual("string");
expect(typeof user.password).toEqual("string");
});

Secondly, let's test our book class.

On this test, we did the same just we did for the User class, by instantiating a new book and saving it to the library database, also on saving it to the database, we went by testing the function response as to what it returns back if it was successful or not. Also as to the library database if it contains the book added and if the book object contains all the necessary book fields.

With this and so many more, testing can help reduce so many bugs and keep our app working well even as to more feature implementations that may come in before production or after production.

Conclusion

TDD is a very vast conceptual topic, with an interesting usage mechanism of its own, and it would be very much impossible for me to talk about everything here.

Please follow the links given for a better understanding of TDD with Javascript

I would go by ending this, with an appreciation to the TDD circle, which has made me not just a better programmer but also understand the fundamentals of a scalable software system with fewer bugs and a maintainable modular codebase.

All complete tests for Bookspy classes, instances, and methods can be found at the repo here. Bookspy Rep

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