Api Test Automation Framework — Jest — Understanding

Vinod Muralidharan
4 min readMay 1, 2021

--

Photo by Joshua Aragon on Unsplash

In continuation to the previous post on Jest, To setup any automation framework (web/api/mobile) using any coding language below are the important modules to be considered. These are general best practices concepts for building any test automation framework:

  • Test runner
  • Assertion Library
  • Reporter
  • Logger
  • core library
  • Env manager.
  • Linter/code quality lib

Now we are going to see why these modules are required to build a test automation framework and why they are considered as best practices. Also we are going to see what packages we are going to use to build the our api test automation framework.

Test runner

Test Runner is the execution unit of the invocation flow. This is where tests actually run.

Yes, you read that right. Test runner, in general does below activity:

  1. Starts the tests
  2. Find the tests to be run
  3. Runs the tests
  4. Finishes the tests

In addition to the above few test runner provides the result either in CLI or in json format.

Now the question comes, how do you choose the best test runner. Below are the characteristics to be considered before choosing any test runner. Is the test runner

  • Flexible — Meaning, are you allowed to extend your test runner in a way you can customise the test automation project
  • Plug and Play — Easy to setup and start writing the test with minimum configuration
  • Test selection — Test runner should be easy in way you can run the subset of test either by providing regex or tagging

In our automation framework, we will be using Jest as the test runner

Assertion Library

Assertion libraries are tools to verify that things are correct. This makes it a lot easier to test your code, so you don’t have to do thousands of if statements

Assertion library are important aspect of any automation test library. This library is the tool used to check if your actual and expected results are matching. If they are matching all good, if not then it throws you the error that you have defined.

Ok, let’s come to the assertion library selection for our automation work and don’t worry about as our Jest has inbuild assertion library 😉

It is called as expect: https://jestjs.io/docs/expect

Reporting Library

Test Report is a document which contains a summary of all test activities and final test results of a testing project. Test report is an assessment of how well the Testing is performed. Based on the test report, stakeholders can evaluate the quality of the tested product and make a decision on the software release.

Yes, and this test report is generated by the reporting library that we use.

While developing test automation framework, we sometimes worry too much about reporting and spend way too much time into it. My person suggestion is to use simple html test reporting lib and start the test framework setup. Based on the stakeholders request we can modify the test reporting library.

For our test framework we are going use simple: https://www.npmjs.com/package/jest-html-reporter

Logger

For many years, logs have been an essential part of troubleshooting application and infrastructure performance

While developing automation framework, we underestimate the use of logging. Many automation framework doesn’t even consider logging. so we need to understand why logging is important for test automation framework:

  • Provides visibility of which stage of test is running
  • Very vital while running tests in CI to find the issues
  • Provides clarity on input and output of each module.
  • Providers timestamp details vital for debugging any issues occurred

In our test automation framework we are going to use Winston: https://www.npmjs.com/package/winston

Core Library

Core Library is the library which is the core of the kind of test automation framework you choose.

  • If Web UI either selenium or cypress
  • If Mobile App then it is Appium
  • If Rest API then GOT or rest Assured

So in our case we are going to test the rest api’s. So we will be using http library: GOT: https://www.npmjs.com/package/got

Env manager

This is another essential and key part which most of the automation frameworks are missing. Testing importantly automation testing occurs in different environments starting from Local, then Dev, QA, Stating and production. We don’t want the details of these environments to be hardcoded in our test code as they are volatile and you cannot change the environments every time you run it.

So in our case, we going to use most popular env manager in nodejs: https://www.npmjs.com/package/dotenv

Linter/code quality lib

Linting is the automated checking of your source code for programmatic and stylistic errors. This is done by using a lint tool (otherwise known as linter). A lint tool is a basic static code analyzer. The term linting originally comes from a Unix utility for C.

Linter or static code analyser should be part of any test automation framework. The benefit of using this is huge compared to time spent in using them. Personally, I regret of not knowing about linting in my initial part of my career. Now why linting:

  • Reliability: Should work consistently without frequent crashes.
  • Consistent Code Style: Should follow consistent code style and naming conventions applicable to the language
  • Maintainable: Should be easy to understand. Maintainable code is easy to extend and add new functionality.
  • Well-tested: Code that has well-written tests tends to have fewer bugs
  • Efficient: Should not use unnecessary resources to perform the desired operations

In our framework, we are going to use eslint: https://www.npmjs.com/package/eslint

So now we had an eagle eye view of setting up the test automation framework conceptually. From next post, we are going to see how we are going to setup the framework using the libraries we have decided to work with 👀

--

--

Vinod Muralidharan

Currently in Berlin, working as senior SDET in LiveIntent Inc. Passionate about Automation testing, Agile, clean code and best practices