Why do we need testing in software and how to get started with Jest

When developers talk about testing in the software development context, they mostly are not talking about manually testing the software, where we perform some operations in our application and use our eyes to see if we are getting the correct result.

Photo by Emile Perron on Unsplash

They are talking about setting up an automated test suite, where we write a little bit of code that performs a few operations in our app and then we add a little more code that asserts we got back the result we were expecting. This helps out developers tremendously especially as our application grows.

The great thing about the automated test suite is we write our test case a single time and we run it as many times as we want. This gives us a lot of confidence that we are not shipping a broken or buggy code in production.

Why do we want to write test cases for our project?

  • Create reliable software
  • Helps with Refactoring, Collaborating, and Profiling.
  • Peace of mind that our software is functional

It vastly helps in collaborating, if the new developer changes some area of the project. The test suite ensures the changes made in the project have not changed the result.

Creating your first test case with Jest JS

Please make sure you’ve node installed.

Let’s setup Jest by opening up the terminal and run the following command

npm i jest --save-dev

The. — save -dev flag is used to install jest as a development dependency.

Add a new script in package.json file under scripts key:value pair; “test”: “jest”

In order to create our very first test file, we must adhere to the naming standard of the file for jest to pick up it as a testing file.

.test extension tells jest that it contains the test cases we want to run.

Now, let’s create our very first test file with the name math.test.js (Yes, it’s a JavaScript file)

Jest framework comes up with the method test() which basically runs the test. The test() takes two arguments, the name of the test and the test to run.

test('My first Test', () => {}

Let’s save the file and run the following command to run the test case.

npm test

The output for the above code

In the output, it is indeed looking through our file math.test.js, and even though our newly created function doesn’t do anything, it is still being considered a passing test case.

Why is it? Well, When we register a test, we write a function providing two arguments. When we run the test, it basically runs that file and checks if all the functions in that file are executing, if the function throws an error then it considers it as the test case failed.

To demonstrate what I mean by it, let’s create a function that we want to fail on purpose. In the same file, add the following code.

test('My failed test', () => {
throw new Error('Failed to execute!')
})

When we execute again by running the command npm test, we get the following output

I’ve two total tests, One passed and One failed.

When we are running math.test.js, all we’re doing is running functions and checking which function passed and which one failed.

Now, the above test case doesn’t add any value to our understanding of how test cases can be useful.

Let’s create a js file with the name math.js

In that file, write a simple function to multiply two numbers.

const multiplyNumber = (num1, num2) =>{
const total = num1 * num2
return total
}

Let’s export this function to our test file by including the export line in our file

module.exports = {
multiplyNumber
}

Now, load lets the function in our math.test.js

const math = require('// Enter Destination of math.js file')

Let’s create a test case for our newly created function. The first argument takes the name of the test case and the second argument computes. Let's compute by passing two numbers and telling what to expect when this function is run. 10 x 3 = 30. If it’s not computing to that then we throw an error.

test('Should get me the right result', () => {const total = math.multiplyNumber(10,3)        if(total!=30){throw new Error('Total number should be 30! Got ' + total)         }})

Comment out all the previous code and run npm test.

Our test case is passed

Great! Our test case has passed.

What if our multiplyNumber() was broken? Let’s say a new developers steps in and edits the function not realizing that he has made a mistake. Let’s quickly mess up our multiplyNumber() to see what would happen.

const multiplyNumber = (num1, num2) =>{
const total = num1 * num2 - num1. //Subtracting num1 again
return total
}

Let’s run again the test suite. What do you see? If you messed up your code like the above then you may be prompted with that the test case has failed. And, indeed it has. The value to return is supposed to be 30 and we will get 20 for the above messed up function.

Jest provides us with another function similar to test() that we can access in our test cases. Let’s replace the line of checking if the value equals to 30 or not with the in-built Jest function.

test('Should get me the right result', () => {const total = math.multiplyNumber(10,3)expect(total).toBe(30) //Single line to check })

With jest, we delegate the task of checking the output by calling expect(), and in that, we provide the value we are expecting something about, In our case, we are expecting total so we pass that in. Now, Jest library ships with dozen or so methods. In our case, we are using toBe() where we are passing the value we expect to get.

I hope you enjoy reading this and for more such article follow me on medium and check out my website Sharjeel Sidd for the things I’ve worked on.

Start Up, I’m not so sure about the rest.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store