Test-Driven Development
Tags: tdd, testing, beginners
Published: 2019-11-01 18:15:56

For that past couple of weeks, I've been trying to figure out the "what?", "why?" and "how?" of test-driven development (TDD). After watching countless Youtube videos and reading some pretty dry documentation, I've been able to find the answer to the first two questions, and have made minimal progress on the last. For clarity, this post will not teach you how to write tests (as I'm still learning myself) but will hopefully guide someone in a helpful direction to learning to write them. So let's get started!

## What is TDD?

According to Wikipedia, TDD is:

*Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: requirements are turned into very specific test cases, then the software is improved so that the tests pass.*

Okay, what I got from that is that tests are written for small chunks of code and then those are tested to make sure they run as intended. That makes sense because I find myself "testing" small parts of my own code (often if-else statements) to make sure that each is responding as it should. 

## Why Test Code?

I'm a visual learner so the first place I look when I have a question is Youtube. My favorite place on Youtube to go for coding questions is Mattias Petter Johansson's (MPJ) account Fun Fun Function. If you haven't watched any of his videos, I recommend you do as they are funny, informational and relevant. MPJ has a 7 part video playlist about unit testing that I found to be particularly helpful in understanding the "why?" of TDD. Early in the video series MPJ equates writing tests to writing down a recipe. You can write tests after you already write the code but it would be like writing down a new recipe after you added all the ingredients - you might not remember the amounts of each ingredient and be unable to replicate it, for code you might have built out a large chunk and are unsure how to test the small segments.

As you may or may not know, I attended a boot camp and most of the labs/assignments we were given had tests pre-written for us. I learned about TDD but never learned how to write tests, which as I soon found out while job hunting, is incredibly important to know. As of now, my understanding of tests is that you write them before you write code, the tests start off red because they're failing, you write code until the tests turn green (passing), and at the end of it all, you refactor. With that, I thought I was ready to jump into test writing...

## How to Write Tests?

This is something that I'm still struggling to learn. I asked my peers and at first, all I got was "Testing is so easy. You just write it and see if it fails." or "All tests are the same. They have a "describe" section and an "it" section and then you write the test. Simple."

Umm...yeah, not helpful at all. So I asked my LinkedIn network if any of them had any good resources for testing that they could share with me. So far what I've seen is that Jest, Enzyme, Cypress.io are some testing frameworks that I should look into. I've successfully written very small React component tests using Mocha and Chai but am excited to see what I can learn about the above-mentioned frameworks. 

I mainly program in JavaScript, React, Ruby, so if anybody reading this has any testing resources related to those, please share them in the comments!!!

Resources:


- [Unit Testing with Fun Fun Function](https://www.youtube.com/playlist?list=PL0zVEGEvSaeF_zoW9o66wa_UCNE3a7BEr)

- [Intro to JavaScript Unit Testing with Mocha JS and Chai](https://www.youtube.com/watch?v=MLTRHc5dk6s&t=75s)

- [JS Unit Testing Guide](https://github.com/mawrkus/js-unit-testing-guide)

- [How to Test React with Jest](https://www.robinwieruch.de/react-testing-jest)

- [How to Test React with Jest & Enzyme](https://www.robinwieruch.de/react-testing-jest-enzyme)