top of page
  • Writer's pictureMarie Cruz

Writing API tests with PactumJS and Jest


PactumJS logo on the left followed by a heart shape and cloud art with "API" text written on it.
PactumJS for API testing

Last month, I attended TestBash UK in person and met the awesome Julia Pottinger for the first time. Julia spoke about Next Level API Automation and she explained important concepts about API testing clearly and also shared various tools to help with API testing.


There are a variety of tools out there that can help you with API testing. For example, I previously blogged about how Cypress can help with API testing. Understanding the goal that you are trying to accomplish when it comes to API testing is more important rather than sticking to one tool so it's important to choose the tool that works best for you and your team.


One of the tools that Julia mentioned during her TestBash talk was PactumJS and I was curious to try it out myself as I haven't used this before. So, in this blog post, I'll explain what PactumJS is, how to get started and how to use it for API test automation.


What is PactumJS?

According to PactumJS's documentation, it is a next generation, free and open-source REST API automation testing tool for all levels in a Test Pyramid that promises a productive and enjoyable experience when it comes to backend testing.


This library took inspiration from two existing tools:

  1. Frisby - an API testing tool built on top of Jest.

  2. Pact - a tool for contract testing which I previously blogged before (Contract Testing with Pact.js and Jest).


Use cases for PactumJS

Apart from API testing, PactumJS seems to also be used for the following uses cases:

  • Component Testing

  • Contract Testing

  • Integration Testing

  • E2E Testing

  • Mock Server

Note that for the above use cases, it's all from an API perspective so E2E testing will not make use of a UI tool.


Installing PactumJS

To get started with PactumJS, you need to have Node installed then simply install the package via NPM.


# install pactum
npm install -D pactum

# install a test runner
npm install -D jest 

PactumJS is not a test runner so you also need to install test runners such as jest, mocha or even cucumber. Feel free to use any test runner that you want but for this blog post, I'll be using jest.


Writing tests with PactumJS

To start, let's create a simple test that checks if it's returning the correct headers for a given API. I'll be using https://jsonplaceholder.typicode.com/todos as my test API.


To do this, you need the following steps:

  1. Initiate a single request to the server. In PactumJS, you can do that by using the `spec` method.

  2. Make a HTTP GET request by using the `get` method.

  3. Once the request is made, you need to validate the response and use assertions such as `expectHeader` to verify that the header is correct.


Code snippet of how to make a HTTP GET request using PactumJS
Making a HTTP GET request with PactumJS

Translating the steps to PactumJS, you get the preceding code snippet above. When the test is run using Jest, you should see an output similar to the one below.


Test output using Jest explaining that there is 1 test passing.
Test output using Jest

Next, I will demonstrate how to do a POST request. Let's say you want to test that you can create a new todo item successfully.


To do this, you can do the following steps:

  1. Initiate a single request to the server.

  2. Make a POST request by using the `post` method.

  3. Include the JSON body that you need to create a new todo item by using the `withJSON` method. For this example, you need to set the title for your todo, the actual body and a user ID.

  4. Validate the response and use assertions such as `expectStatus` to verify the status code and also `expectJsonLike` to verify a partial JSON object.


Code snippet of how to make a HTTP POST request using PactumJS
Making a HTTP POST request with PactumJS

If you want a stricter assertion, rather than using `expectJsonLike`, you can also use `expectBody` and provide the entire JSON response body.


The last example that I want to demonstrate is deleting a todo item which is quite simple to do. To do this with PactumJS, you just need to use the `delete` method, pass in the API call and validate the status code.


Code snippet of how to make a HTTP DELETE request using PactumJS
Making a HTTP DELETE request with PactumJS

Wrap up

As you can see from the above examples, PactumJS is an easy to use tool and provides a nice library to test your APIs. The methods are self explanatory and descriptive enough to guide you when it comes to writing your tests. I highly recommend going over their API documentation for comprehensive information on the different methods that they have.


The examples above only demonstrate API testing. I'm curious to see how it works with the other use cases mentioned above so I'll cover them in future blog posts! 😊


All code examples can be found on this Github repository - pactum-js-demo.

2,265 views3 comments

3 comentários


Somasundar Alapati
Somasundar Alapati
02 de nov. de 2022

Thanks for sharing such a simple and nice tool.

Curtir

MONISH CORREIA
MONISH CORREIA
19 de out. de 2022

Hi Marie, Thanks for the blog post.

Is html report available after the test run? Request you to cover this in the next blog post, if available.

Curtir
Marie Cruz
Marie Cruz
19 de out. de 2022
Respondendo a

Hi Monish, this package https://www.npmjs.com/package/jest-html-reporter should be able to help you with having an html report. I'll try to cover it but have a look at this package as well :)

Curtir
bottom of page