Front End Chapter Workshop to learn the ins-and-outs of test driven development
Put your laptop on Do Not Disturb (We don't want those Teams messages coming through).
Quoting Kent Beck, "Test-driven development is a way of managing fear during programming". But why would you fear your own programming? Why would you fear your own code? Maybe it's not the code... maybe it's you that you should be afraid of. In my experience with TDD, I've learned that I can't trust myself without having a test to prove I programmed something right. That may sound a little extreme but it's true.
All of us know that we need tests whether that's because
- We know we should do it (but secretly find testing tedious)
- Our team and leadership expects good code coverage
- It's "apparently" a better way to program
That's how I felt and the morale on my team wasn't helping either. We had a saying, "Coding the logic takes an hour but testing afterwards takes three days". Ever felt like that?
Let's flip that on it's head so that you start with a test, code a little bit, and then make the test pass. This is called the Red, Green, Refactor method.
It looks like this:
- Red: Write a failing test
- Green: Write the smallest amount of code to make it pass
- Refactor: Remove any duplication
Why?
There's tons of benefits to TDD and I can testify to them. Here's a few:
- You're going to increase your code coverage by default
- You're going to write classes that are more decoupled, code that's easier to test, and cleaner code because you're constantly refactoring
- Refactor without the ๐ฆ
- Ship less ๐ชฒ
- My personal favorite: Wishful coding
But you may think this sounds just like writing tests in general. TDD is not just unit testing. There's a difference: TDD is writing tests before you write the code.
TDD's strength lie in the unit test portion of the testing pyramid
You want to write tests that run quickly so you shorten your feedback loop. You can hold off on the integration and E2E testing until later.
Enough talk. The best way to learn is through practice.
Creating Dollars, Peso, and abstracting them.
Go to your development folder and clone the repo
git clone https://github.com/patrady/fec-tdd-workshop.git
Open up the repo in your favorite IDE or if you're using VSCode then
code fec-tdd-workshop
Get everything setup on your machine and notice that there is a failing test
yarn install
yarn test