Week 6 at Command Shift

Coming to the end of week 6, this week was all about Test Driven Development. Not going to be covering a tonne today so we can jump right into it. As always check the Command Shift website for any details on the course itself!

Test Driven Development

What is Test Driven Development(TDD)? TDD is what we do to test our code as we create it. We write out a test using a testing framework, for which we used Jest, to pass in some descriptions of what we want our code to do. I'll show an example below:

const { booleanToWord } = require("../src");

describe("booleanToWord", () => {
  it('returns Yes when passed true', () => {
    expect(booleanToWord(true)).toBe('Yes')
  })
  it('returns No when passed false', () =>{
    expect(booleanToWord(false)).toBe('No')
  })
});

Ok, what're we looking at here? This is what a test looks like, and what we're asking this test to do is: When we call the function booleanToWord, it will return Yes when passed true or No when passed false. This was one of the earlier challenges we had in learning TDD. Next, we had to write the code to pass the test:

const booleanToWord = (boolean) => {
  return boolean ? 'Yes' : 'No'; 
};

module.exports = booleanToWord;

Pretty straightforward code. Using the ternary operator (?) allows me to shorten a regular if statement to one line. When we run the test framework for this, it comes back a pass! Now that we have the test passing, we can add extra tests for different circumstances and write new code as we go to make sure everything is testing as it should be. And then we'd write code to make sure it passes our test, and then we'd write more tests and more code etc. The cycle continues from there on.

A wrap on TDD week

This week we did an exercise during our lecture which involved TDD and we split off into groups of around 3-4 with a tutor. Our task was to build a barcode scanner and write tests for it. The tutor was typing out the code for us and we were all giving suggestions and ideas for the code. This was a great exercise as it happens on the job and it was good to solidify some of the knowledge when thinking through the exercise. Our tutor Jenny also gave us some good tips (typing out the layout of the code before filling it out was a hidden gem) and it was fun to interact with the other people on the course.
TDD was great to learn and dig into as it'll be a large part of our career moving forward. Over the following weeks, we will be digging into Object Oriented Programming(OOP) and building some projects.
Thanks for reading!