Table Of Content
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.
Test Driven Development is a process we take to test our software program,
this process is consist of three steps Test, Code then Refactor.
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.
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.
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.
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.
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
Then change the test command in the package.json file to jest like this
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
Ok, good now we will navigate to index.js file to create our “sum function”
then export it to use in our testing file
Then we import it into our test file and run the command again
1bash
2npm run test
3
Now assume the user has added 1 parameter only instead of 2,
let’s add this scenario to our testing code, and run it.
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.
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
1num1
1num1 + 0
1num1
2
We have learned how to create a simple test,
now let’s take some examples of a real project.
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.
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
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.
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.
Service
© 2025 Elroby.org All rights reserved