Don't cheat when using TDD!

What’s TDD and how to setup jest in nodeJS

Today we will have a look at what’s TDD and why some developer teams use it. Personally I haven’t used TDD a lot, I think it slows down the development a bit, but I can see the upsides of it which we will cover later. Moreover writing tests, seeing them fail and then building the code from scratch to pass them is a lot of fun and feels like an achievement at the end of the day. So let’s dive together to TDD!

So what is TDD?

Test driven development is about writing tests before you even write a single line of code. It was originally invented by Kent Beck as part of his extreme programming methodology, back in the 1990s and has continued to gain adherents ever since.

TDD diagram

Before each functionality you want to add to your code, you first write a bunch a tests, run them and see that they are all failing. Now failing here is good, we want that, because there is still no code for the tests to be passing. We call this part the red part, because all our test are failing.

After you have written your tests and hopefully covered all edge cases and what not, you can now write the implementation to pass these tests and make them green. This part is usually called the green part of TDD, because after you are done with the implementation all you tests should be green therefore passed.

See also  The easiest way of adding env variables to NodeJS

The last part of TDD is called refactor and is usually given the blue color. Now you don’t want to change the functionally of the code in this part, you just want to make it nicer, more readable and remove redundancy from your code. What that means you don’t want to have a single red tests after refactoring, you want to still pass all them.

TDD best practices

  1. Create a simple test and see it fail. You can’t go to part 2 until the test fails.
  2. Implement a simple solution for the test and see it pass.
  3. Refactor your code, until you are satisfied with it. Now see if you the code is still passing the test even after your changes.
  4. Go to step 1

Basic Jest setup in nodeJS

To get started, we need a basic setup of nodeJS with babel. Check out this tutorial for that.

Now we need to install the Jest library.

npm install jest --save-dev

Add to package.json under “scripts” a new script called “test” that looks like this:

"test": "jest ./dist/index.spec.js"

Then we add to the src folder a new file called index.spec.js. In this file we will specify our first tests.

Add these lines of code to the new file:

describe("Tests", () => {
  test("Jest is working", () => {
    expect(1).toBe(1);
  });
});

What this does is it describes a set of test called “Tests” and then a single test is defined called “Jest is working” and in it we check if 1 is equal to 1. This is always true so our test is passing and is green! Obviously this test is only for checking out if our jest setup is working, otherwise the test doesn’t make much sense.

See also  How to create a new Node JS library

Now if we want to start the test, all we have to do is run the script we specified earlier in package.json:

npm run test

Congratulations! You have just successfully run your first test in nodeJS! 🥳🥂

Hopefully there are many more to come, as you can see writing tests and making them pass is a whole bunch of fun! 🤩

Conclusion

It can be daunting at times to get into TDD, but in the end, it’s quite simple, really! As I’ve said earlier it might slow down the development by a bit, but you as a programmer get to think about the problem in many more ways by using TDD. The code becomes more flexible and modular as you refactor it and refactoring in it self becomes easier. Also you get a bit better understanding of the program design. And most likely you find a lot more edge cases and errors in your code than just by regular coding.

In the next article we will look at how to implement TDD in a simple tennis scoring system app.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *