Skip to main content

Command Palette

Search for a command to run...

Week 6 - Test Driven Development (TDD)

Updated
8 min readView as Markdown
Week 6 - Test Driven Development (TDD)

A week of lots of new concepts and terms, culminating in some more kata exercises.

So what did week 6 look like?

Testing, testing, anyone there?

I thought I would do a brief (ish) run down of some of the concepts from our Test-Driven Development week.

Node.js - Node (aka Node.js) is a JavaScript runtime which lets you run JavaScript code on your computers without the need of a browser! It is open source and free to use.

Node Package Manager (NPM) - NPM is the Node Package Manager, the world’s largest software registry. It helps install, upgrade and configure packages for your JavaScript projects from npmjs.com. NPM works with Node and is installed when you install Node. There are other Package Managers too, they can be global or language specific.

Node_modules - the heaviest thing you will ever lift (do you even lift though?). This is a directory created by NPM and can contain thousands of packages and their dependencies. These should not be committed to git.

package.json - a configuration file which sits at the root of JavaScript projects. It contains information about the project such as the name of the project, version, author, scripts and license. It also contains a list of dependencies required for the software to run, and other specifics.

Dependencies - are modules which you need to either develop or use the project you are building. There are two main types of dependencies, production dependencies (simply called dependencies) and devDependencies (for development).

Test-Driven Development (TDD) - a development approach where you write a failing test before you write the code. Then you add in the functionality gradually, running the tests to see the errors and building on what you’ve written. This cycle is called red, green, refactor.

TDD cycle

Unit Testing - this is when you test each method of an object separately, using jest. Each test module is called a unit. Unit testing can be split into the four phases of set up, exercise, verify and teardown. You can read about that here.

Jest - a JavaScript testing framework, jest requires no configuration & you can install it with NPM. The tests live in a tests directory and are commonly called [filename].test.js, so my test file for my current project is pets.test.js

Refactoring - this is thinking about how to improve your existing code, making it more efficient or clear. In testing, this could be considering the characters of a good test, or in other instances, how you could accomplish the output with more descriptive or faster code, or can you remove some duplication.

Don't Repeat Yourself (DRY) - this is a development principle aimed at reducing repetition or duplication of the same patterns. I’ve discovered there is also Write Everything Twice (WET), and there are quite a few articles out in the ether discussing the merits of both!

Okay, so we are learning a lot!

Honestly, testing seemed quite exciting for me. I like the step-by-step process and writing the test first gave me something to clearly aim for when writing code to work with it. We pair-programmed again for the assignment and you can visit my github to see the work we did. I’ve also included a link about git pairing so you can see how it works.

I also enjoyed the practical application of all the learning. Concepts seem to be coming together a little better and I feel my understanding of the code is improving. I find reading the code out loud really helps insert rubber duck here.

To start writing our tests we followed this basic structure:

it('adds two numbers', () => {
  expect(add(1, 2)).toEqual(3);
});

Starting with the keyword it (you can also use test) this is a function which indicates a single test. It takes a string and a callback function as arguments. The string describes what the test is doing and the callback executes the code for the test.

expect is a function which allows you to make statements about a value and it takes that value as an argument, it then gives you access to matchers which let you validate different things.

toEqual is the matcher in this example, there are many in the jest documentation

describe is the function which takes two parameters: the title of the test and test to be executed. describe allows you to group test togethers, creating a common setup for tests.

describe('add', () => {
  it('adds two numbers', () => {
    const result = add(1, 2);
    expect(result).toBe(3);
  });
  it('adds negative numbers', () => {
    expect(add(-1, -2)).toBe(-3);
  });
});

Here’s to week 7 of bootcamp!

My Key Take Aways

Get your fingers on the keyboard - Sometimes when I feel a bit intimidated by all the code, I go back a few steps and refamiliarise myself. One thing I can safely say after seeing several tutors and devs code in lecture is that everyone has to look things up, everyone seems to forget the odd bracket and we all make the odd typo. The key is to keep going!

Be present - It’s hard concentrating on a lecture after 7 hours of work, but the least I can do is give my tutors my concentration and my class-mates the support they deserve. I try and contribute where I can and be honest in my standups.

Acknowledge when you need a break - Sitting yawning at the computer staring at GitHub/ freeCodeCamp/ stack overflow will do you no favours. Sometimes you just have to call it. Get away from the screen, have a walk, a brew, a bath (whatever works!). I have worked really hard to be okay that I can be perfect 24/7 and I feel much happier for it. And Pomodoro is a life saver (I know I’ve said this before!).

More from this blog

Jennifer Cant

15 posts

I'm a full-time PA learning to code on the Fast Track programme at Manchester Codes. I am using Hashnode to document my learning. I live in West Yorkshire, England, in a house full of art and books!