API automation testing is an essential part of software development. It ensures that the APIs are working as expected and delivering the intended results. However, the process of API testing can be time-consuming and error-prone. PactumJS is an open-source library that can help streamline the API testing process by enabling developers to write test cases that mimic real-world API calls. In this article, we’ll take a closer look at how PactumJS can be used for API automation testing.
PactumJS is a lightweight, free and open-source REST API automation testing tool, that provides all the necessary ingredients for the most common things to write better API automation tests in an easy, fast & fun way.
PactumJS provides great documentation for API Testing (including GraphQL), Component Testing, Contract Testing and also a Mock server feature.
Note: PactumJS is not a test runner. It needs to be used alongside with a test runner like mocha, jest, jasmine, cucumber or build your own if your heart desires, pactum should work out of the box.
Prerequisites
Getting Started with PactumJS
To get started with PactumJS, you first need to install it. You can install it using the following command
npm install -D pactum mocha npx
Demo Apps
For demo purpose, we will be using both free to use QA-Practice REST application and also QA-Practice GraphQL app
If you have Docker installed on your machine, feel free to start the REST API application on local machine with this simple CLI command
docker run -d --rm --name qa-practice-api -p8887:8081 rvancea/qa-practice-api:latest

The same goes for QA-Practice GraphQL App: using Docker , feel free to start it on local machine with this simple CLI command
docker run -d --rm --name qa-practice-graphql -p 5000:5000 rvancea/qa-practice-graphql

Writing a basic test scenario

In this example, we are making a GET request to the QA-Practice REST app and assert that it returns a 200 response status code. PactumJS provides a fluent syntax that makes it easy to write test cases and it is packed with a rich set of features for making HTTP requests & validating the server responses. Head over to Requests and Assertions for more details.
Writing more complex tests - JWT Authorization

In this example, we are using the before hook to generate an authorization token (JWT) and store its value in ‘authToken’ variable.
Then we are using the stored token to make a GET Employees request (that requires authorization) and check that it returns a 200 status code (OK).
Finally, we add a negative test as well, trying to call the same GET Employees endpoint, but without passing the token as Authorization header, therefore the expected response status code is 401 (unauthorized).
Writing a GraphQL test

In this example, we are sending a request and assert that the response status code is 200 and the response contains a song title called ‘Nothing else matters’.
Running tests
The tests get executed with a simple CLI command, as it follows:
npx mocha example.test.js

Reporters
By default, pactumjs uses spec reporter, meaning that the test execution will be displayed in the CLI. However, you have the flexibility to install other types of reporters as dependencies (e.g. Mochawesome for HTML reports)
Asserts
PactumJS’s documentation provides lots of asserts that you can find HERE
In the next picture, we will see some of them that we can use in our tests

Conclusion
By using PactumJS, you can write more comprehensive and effective API tests, ultimately leading to more reliable and high-quality software.
Enjoyed this article? Make sure to subscribe to my YouTube Channel for more Test Automation tutorials, and follow me on LinkedIn and Twitter for regular insights.
Looking to improve your test automation skills?
I’ve created a personalized 1-on-1 Mentoring program refined to boost YOUR skills. Reach out at iamrv@razvanvancea.ro for more details and together will create a learning path adapted to your current skills and goals that you are aiming for, in a timely-efficient manner🚀
