logo

Introduction into TDD " Test Driven Development "

TTD Cover

Ali Adel Elroby

Front End Developer

Table Of Content


1- Introduction2- What’s Test Driven Development?3- Why You Should Use It?4- Start Your First TDD Code5- More Examples

Introduction

Sometimes when you're working on a big project with many details and is complex, you're missing some test causes or you're facing an error.

TDD comes to help us to avoid these problems and to make our
code cleaner and described.


What’s Test Driven Development?

Test Driven Development is a process we take to test our software program,
this process is consist of three steps Test, Code then Refactor.

Test

This is our first step in TDD, here we specify our needs from the code,
we’re asking ourselves, what should the code do?

Also, we’re thinking about all the scenarios the code should handle it,
then we turn those needs into a test code and run it.

Code

After running the test causes we are seeing a test case that failed,
so in this case, we add some code to make this test pass successfully.

Refactor

Awesome, now we have created the test causes, and then make all of
them passed successfully, now we need to refactor our code,

To make it cleaner and remove any duplicates.


Why You Should Use It?

Because Test Driven Development helps us to maintain our code,
also to aspect any scenario that can happen in the Test part
by writing all the scenarios that our code should handle it,
before we start coding.

Then we code our software program and make our code
has the ability to handle every single scenario that we have written.

It helps us to make our code cleaner, by reducing the number of lines
that we have written, and remove all the duplicated code
that happened in the Refactor part.

I think you are convinced now, of how is essential and useful
to use TDD in our projects, so let’s create our first TDD.


Start Your First TDD Code

In this approach, I will use Jest testing library for Javascript,
that you can install it with the following command

We can start our project by creating a new folder called then open it and run

npm init

Then change the test command in the package.json file to jest like this

jest

Ok, that’s good, now we need to create a test file called index.test.js

to contain our testing code, we need another file called index.js that has our core code.

Let's start working, we will create a simple function that takes 2 parameters “num1” and “num2”, and return their sum.

First, we will create a test case to test our sum result, and then run our test by typing

1bash
2npm run test

test

Ok, good now we will navigate to index.js file to create our “sum function
then export it to use in our testing file

Screenshot from 2022-10-29 08-27-37

Then we import it into our test file and run the command again

1bash
2npm run test
3
to see the magic, our tests have passed successfully.

Screenshot from 2022-10-11 10-03-04

Now assume the user has added 1 parameter only instead of 2,
let’s add this scenario to our testing code, and run it.

Screenshot from 2022-10-11 10-04-53

Ops, we have a problem here, our function returned NAN instead of 5,
let’s fix that, we can say for our function if num2 === undefined then return num1,
and try to run the code again.

Screenshot from 2022-10-11 10-07-39

Awesome, we have passed all the test cases, but we can think of
refactor our previous code, we can use Default Parameter for num2 to be zero

Now if the user has not added

1num2
then will return
1num1
or
1num1 + 0
which equals
1num1
2
let’s run the test again 

Screenshot from 2022-10-11 10-10-20

We have learned how to create a simple test,
now let’s take some examples of a real project.


More Examples

Let’s create a simple CRUD system that helps us manage users
as we said before, we will start with the test causes and what should our code do.

So we will need four functions “ Add, Get, Update, and Delete “ 
I will create the test causes that our code should handle and run the tests.

Screenshot from 2022-10-29 09-27-36

Ok, that’s good for now, let’s create our users object that has our functions
and then export it to use in our test file, my

1index.js
file now became like this.

Screenshot from 2022-10-29 09-29-06

Now let’s import the users object in our test file to test our CRUD system,
and now as you will see, our test cases passed successfully.

Screenshot from 2022-10-29 09-29-41

Now we should refactor your code,
but I think it's good right now so we will skip this step.

I think now you have known, how is important to use test-driven development
In your projects, to make your code cleaner and also to aspect any scenario
your code should handle.

I hope you learned something from my post today,
also If you are interested in these types of posts you can subscribe to my newsletter
to notify you, when I create a new post.

Thank you for your reading and I hope you get a nice day.


Send to my inbox

You can subscribe in my newsletter to see my weekly posts.

To Support Me You Can

Ali Adel Elroby | Buy Me A Coffee

Subscribe Newsletter

logo

E-Learning, E-Commerce, Saas
Front end development

Service

  • Contact Me
  • Works
  • Blog

Contact

Contact@elroby.org+201507262414

© 2025 Elroby.org   All rights reserved