Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pactum

Package Overview
Dependencies
Maintainers
1
Versions
112
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pactum

REST API Testing Tool for all levels in a Test Pyramid

  • 3.7.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
170K
decreased by-3.44%
Maintainers
1
Weekly downloads
 
Created

What is pactum?

Pactum is a REST API testing tool that allows you to write tests in a simple and readable manner. It supports various types of testing including functional, end-to-end, and performance testing. Pactum is designed to be easy to use and integrates well with other testing frameworks.

What are pactum's main functionalities?

API Testing

This feature allows you to test REST APIs by making HTTP requests and validating the responses. The code sample demonstrates a GET request to a sample API and checks the status code and JSON response.

const pactum = require('pactum');
pactum.spec()
  .get('https://jsonplaceholder.typicode.com/posts/1')
  .expectStatus(200)
  .expectJson({
    userId: 1,
    id: 1,
    title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit',
    body: 'quia et suscipit\nsuscipit...'
  })
  .toss();

Mock Server

Pactum allows you to create a mock server to simulate API responses. This is useful for testing scenarios where the actual API is not available. The code sample shows how to set up a mock server that responds to a GET request.

const pactum = require('pactum');
const { mock } = pactum;
mock.addInteraction({
  request: {
    method: 'GET',
    path: '/api/users'
  },
  response: {
    status: 200,
    body: [{ id: 1, name: 'John Doe' }]
  }
});
mock.start(3000);

BDD Style Testing

Pactum supports Behavior-Driven Development (BDD) style testing, which makes tests more readable and easier to understand. The code sample demonstrates how to write BDD style tests using Given, When, and Then.

const pactum = require('pactum');
const { Given, When, Then } = require('pactum').bdd;
Given('a user exists', () => {
  pactum.spec()
    .get('https://jsonplaceholder.typicode.com/users/1')
    .expectStatus(200);
});
When('I get the user details', () => {
  pactum.spec()
    .get('https://jsonplaceholder.typicode.com/users/1');
});
Then('I should see the user details', () => {
  pactum.spec()
    .get('https://jsonplaceholder.typicode.com/users/1')
    .expectJson({
      id: 1,
      name: 'Leanne Graham'
    });
});

Other packages similar to pactum

Keywords

FAQs

Package last updated on 12 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc