### Install PactumJS and a test runner Source: https://github.com/pactumjs/pactum/blob/master/README.md Instructions to install PactumJS as a development dependency and a test runner like Mocha, or to initialize a project using npx. ```shell # install pactum as a dev dependency npm install --save-dev pactum # install a test runner to run pactum tests # mocha / jest / cucumber npm install --save-dev mocha ``` ```bash npx pactum-init ``` -------------------------------- ### Perform simple API tests with PactumJS using Mocha Source: https://github.com/pactumjs/pactum/blob/master/README.md Demonstrates how to write basic API tests using PactumJS with Mocha. Includes examples for GET and POST requests, checking status codes, and sending headers and JSON bodies. ```js const { spec } = require('pactum'); it('should be a teapot', async () => { await spec() .get('http://httpbin.org/status/418') .expectStatus(418); }); it('should save a new user', async () => { await spec() .post('https://jsonplaceholder.typicode.com/users') .withHeaders('Authorization', 'Basic xxxx') .withJson({ name: 'bolt', email: 'bolt@swift.run' }) .expectStatus(200); }); ``` ```shell # mocha is a test framework to execute test cases mocha /path/to/test ``` -------------------------------- ### Configure and run PactumJS as a standalone mock server Source: https://github.com/pactumjs/pactum/blob/master/README.md Illustrates how to use PactumJS to set up a standalone mock server. It demonstrates adding an interaction to mock a GET request to '/api/projects' with a predefined JSON response and starting the mock server on a specified port. ```js const { mock } = require('pactum'); mock.addInteraction({ request: { method: 'GET', path: '/api/projects' }, response: { status: 200, body: [ { id: 'project-id', name: 'project-name' } ] } }); mock.start(3000); ``` -------------------------------- ### Integrate PactumJS with Cucumber for API testing Source: https://github.com/pactumjs/pactum/blob/master/README.md Shows how to integrate PactumJS with Cucumber using Gherkin syntax for test scenarios and JavaScript for step definitions. This allows for behavior-driven development (BDD) style API testing. ```gherkin Scenario: Check Tea Pot Given I make a GET request to "http://httpbin.org/status/418" When I receive a response Then response should have a status 418 ``` ```js // steps.js const pactum = require('pactum'); const { Given, When, Then, Before } = require('@cucumber/cucumber'); let spec = pactum.spec(); Before(() => { spec = pactum.spec(); }); Given('I make a GET request to {string}', function (url) { spec.get(url); }); When('I receive a response', async function () { await spec.toss(); }); Then('response should have a status {int}', async function (code) { spec.response().should.have.status(code); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.