In this blog, we have picked a library from the JavaScript testing framework, Jest to explain how to do unit testing with some interesting examples. We have utilized some of the key features of Jest library.
We previously explained the purpose of testing and how it can help improve the quality of your code along with explaining the fundamental ways to test out JavaScript apps. In the series, we have picked one library to explain how we can do Unit Testing with some interesting examples, utilizing key features of Jest Library.
"Unit testing is a way to validate that individual units of code are working correctly. By writing unit tests, we can ensure that our code is reliable and free of bugs. This can save us time in the long run, because it's easier to catch and fix problems early on in the development process.”
We will need to install the package and create a configuration file. Here are the steps to follow to set everything up:
1. Install Jest by running npm install --save-dev jest or yarn add --dev jest. This will add Jest to your project as a development dependency.
2. npm install jest --global To run jest on command line
3. Create a configuration file by running jest --init. This will prompt you to answer a few questions about your project, and then generate a jest.config.js file in the root directory of your project.
4. Open the jest.config.js file and modify it as needed. You can specify options such as the test environment, test file patterns, and test transforms.
Here is a detailed info about jest.config.js file:
We can customize the configuration file to suit our needs. For example, one can specify additional options such as the test environment, test reporters, and test coverage thresholds.
5. Create a package.json file. This is where one can define scripts for running your tests.
6. In the package.json file, add a script for running the tests. For example:
This script allows us to run our tests by typing npm test or yarn test on the command line.
Now we have the basic setup of jest & we are ready to test our app.
Let's understand what is happening here:
This test case is testing the add function, which is expected to take two numbers as arguments and return their sum. The test case verifies that the add function produces the correct result when given the input 1 and 2.
In simple words this example is equivalent to this:
Just the difference is libraries have the all boiler code written for us so that we can directly use this as functions and avoid writing the same code again and again.
Mocking is just like mimicking the behavior functions that you don’t want to be computed. This can be useful for testing code that depends on external resources such as APIs or database queries because it allows you to test the code without actually making the external calls.
Let’s understand this with an example.
Say we have to write the unit test case to check whether there is debit transaction from a bank account, a bank account module will look like this
By looking at this file you can understand i have one method - getAccount , to get account details for a particular id from database which gives me account balance in the response
And the second method is debitTransaction which results in debiting the corresponding amount ( Specified in the argument ) from the specified bank account id ( Specified in the argument ) and returning True if successfully debited the amount and return false if there is no sufficient balance in the account.
To avoid making these database queries while testing our application, we can Mock these methods to return the same data without actually computing these methods, and you can do like this in jest and test the functionality.
In this test case we are mocking ( Overriding ) the getAccountfunction from the bankAccount module to return an object with the correct id and balance properties based on the input arguments.
In this example
This test case is testing the debitTransaction function to ensure that it correctly updates the balance of an account and returns the correct result based on the input arguments.
This is how we can mimic or mock behaviours of the functions in unit testing
If we want a node to watch for changes to our .js files so we don't have to keep entering $ npm test over and over, simply add the following to the test script in package.json, run $ npm test, and the tests will run automatically anytime you make changes to a .js file.
In this series of blogs, we will soon be talking about Integration testing. To know more reach out to us at contactus@coditation.com.