Beginner’s guide to testing with Jest

Dipak Singh
walkin
Published in
5 min readMay 27, 2019

As a software tester, I test many front-end and back-end applications. For API testing, I use Postman, which I trigger manually. It is a tedious and time-consuming job to perform regression testing after every change. So I wanted to learn automation of test cases. Having no prior knowledge of programming, I went to YouTube and searched for tutorials on automation. I also started following many blogs on testing.

I started looking for a good framework to learn and I found Jest. I found it super useful for a non programmer like myself, as it’s easy to understand and write test cases with.

What I liked most in Jest is it is Zero configuration testing platform. Due to zero configuration test execution time using jest is very less as compared to other existing frameworks.

Below are some links that helped me to learn jest:

https://jestjs.io/docs/en/getting-started

https://blog.logrocket.com/testing-with-jest-from-zero-to-hero-85ce0e9cc953

https://www.codementor.io/pkodmad/dom-testing-react-application-jest-k4ll4f8sd

https://www.sitepoint.com/test-react-components-jest/

End to end Testing:

End to end testing is a testing methodology to test an application flow from start to end. The purpose of end to end testing is to simulate the real user scenario and validate the system under test and its components for integration and data integrity.

Example of End to end Testing:

1.Testing Gmail API for Sign-in

2.Testing Gmail API for sending a new message

Instructions to install and start writing test cases in jest:

VS code is the recommended IDE to work with.

JEST is built on JASMINE and it comes with inbuilt Assertion framework and Hooks(It’s a place in code that provides different behavior or to react when something happens) with the help of Hooks we can maintain the code structure which is simple and easy.

1) Install JEST

Run the following command to install JEST

npm install — save-dev jest

Add the following line under “scripts” in package.json file

“test”: “jest”

2) Install graphql-request and axios library

GraphQL Request library is used to make graphql requests(i.e Query and Mutation). It is the Most simple and lightweight GraphQL client and similarly axios is for rest APIs.

Run the following command to install GraphQl Request library

npm install — save-dev graphql-request

Writing Test Cases:

In the old approach, I write test cases in excel sheet and execute them manually using postman.

I would start with the same step for the new approach also. Having a list of test cases would serve as pseudo-code for my automated test cases.

  1. In Jest to execute the test cases first we need to create __tests__ folder in the project root folder

2.Start creating test cases files inside __tests__ folder for each API.This informs jest to look for test cases in this folder, while running.

Executing Test Cases:

Run the command “npm test” or “npm t” to execute all the test cases.

To run only one file, you need to specify that specific API name along with npm test

Ex: To run generateOTP.js file the command is “npm test generateOTP

Hurray! My tests are running!

CI/CD: It is Continuous Integration and Continuous Delivery. It involves continuously building, testing, and deploying code changes at every small iteration, reducing the chance of developing new code based on bugged or failed previous versions.

We used Gitlab Repository for CI/CD.

How GitLab CI/CD works

  1. To use GitLab CI/CD, all you need is an application codebase hosted in a Git repository, and for your build, test, and deployment scripts to be specified in a file called .gitlab-ci.yml, located in the root path of your repository.

2. In this file, you can define the scripts you want to run, define include and cache dependencies, choose commands you want to run in sequence and those you want to run in parallel, define where you want to deploy your app, and specify whether you will want to run the scripts automatically or trigger any of them manually. Once you’re familiar with GitLab CI/CD you can add more advanced steps into the configuration file.

3. To add scripts to that file, you’ll need to organize them in a sequence that suits your application and are in accordance with the tests you wish to perform. To visualize the process, imagine that all the scripts you add to the configuration file are the same as the commands you run on a terminal in your computer.

4. Once you’ve added your .gitlab-ci.yml configuration file to your repository, GitLab will detect it and run your scripts with the tool called GitLab Runner, which works similarly to your terminal.

5. The scripts are grouped into jobs, and together they compose a pipeline.

Example Gitlab CI file

I have created the following .gitlab-ci.yml file in my project and it tells Gitlab to run my test cases. Whenever change happens test cases will run automatic and verify whether all test cases getting passed or not.

Write below script in .gitlab-ci.yml file:

# https://hub.docker.com/r/library/node/tags/

image: node:latest

test_async:

script:

- npm install

- npm test

Later we can improve our script once we understand better.

Once all test cases run results will show like below attached screenshots.

--

--