Socket
Book a DemoInstallSign in
Socket

@heetch/cypress-mock-openapi

Package Overview
Dependencies
Maintainers
6
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@heetch/cypress-mock-openapi

Cypress command to stub network requests using OpenAPI examples

1.0.2
latest
Source
npmnpm
Version published
Weekly downloads
203
37.16%
Maintainers
6
Weekly downloads
 
Created
Source

@heetch/cypress-mock-openapi

Push

This package contains a Cypress plugin and command to mock and validate responses in tests using an OpenAPI (Swagger) contract.

Installation

npm install --dev @heetch/cypress-mock-openapi

OR

yarn add --dev @heetch/cypress-mock-openapi

Configuration

After installing the package, configure Cypress with the following environment variables:

{
  "openapiPath": "test/openapi.yaml",
  "apiPrefix": "http://api-prefix" // Optional
}

The apiPrefix can be set globally but also overridden in both commands (or omitted completely).

In your Cypress plugin file:

import getOpenAPIResponse from '@heetch/cypress-mock-openapi/dist/plugin';

module.exports = (on) => {
  on('task', {
    getOpenAPIResponse,
  });
};

Either in your support/index.js file or commands.js (for a standard configuration):

import '@heetch/cypress-mock-openapi';

Basic usage

cy.mockWithOpenAPI(options)

This command will:

  • Intercept a network request based on the passed options
  • Search for the matching operation in the configured OpenAPI file
  • Return the response of the operation and method based on the exampleKey

Note: The first example defined in the OpenAPI contract will be used if none are specified

# OpenAPI operation for GET http://my-api.com/users
examples:
  ACTIVE:
    value:
      users:
        - name: Paco
          active: true
it('Displays a list of users', () => {
  // Set up the mock for GET http://my-api.com/users
  cy.mockWithOpenAPI({
    apiPrefix: 'http://my-api.com',
    url: '/users',
  }).as('getActiveUsers');

  // When the page loads, fetch /users
  cy.get('html').then(() => fetch('http://my-api.com/users'));

  // Await the results of the interception and make assertions on the mocked response
  cy.wait('@getActiveUsers').then((interception) => {
    expect(interception.response.statusCode).to.eql(200);
    expect(interception.response.body).to.eql({
      users: [{ name: 'Paco', active: true }],
    });
  });
});

cy.validateWithOpenAPI(options)

This command can be used to validate that your API returns a response that conforms to the OpenAPI contract (both the request and the response). It will perform the request with the provided options and return the actual response if the validation passes.

If the validation fails, an error will be thrown that contains a list of contract violations. For example:

it('Throws an error if the validation fails', () => {
  cy.validateWithOpenAPI({
    url: '/users',
    headers: {
      Authorization: 'Password',
    }
  });

  // Throws an error with the following information returned:
  {
    message: "The response doesn't match the OpenAPI contract",
    violations: {
      "body.users[0]": "required: should have required property 'age'",
    }
  }
});

Note: This command doesn't intercept requests

Options reference

mockWithOpenAPI(options)

NameTypeOptionalDefaultExampleDescription
apiPrefixStringfalseundefinedhttp://my-api.comA prefix for API calls. Can also be configured global using Cypress env variables
urlStringfalseundefined/users /dogs?cute=trueThe pathname and query parameters of the request
exampleKeyStringtrueundefined'OK'By default the first example will be used if none are specified
methodStringtrue'GET'https://developer.mozilla.org/en-US/docs/Web/HTTP/MethodsAny supported HTTP method

validateWithOpenAPI(options)

NameTypeOptionalDefaultExampleDescription
apiPrefixStringfalseundefinedhttp://my-api.comA prefix for API calls. Can also be configured global using Cypress env variables
urlStringfalseundefined/users /dogs?cute=trueThe pathname and query parameters of the request
methodStringtrue'GET'https://developer.mozilla.org/en-US/docs/Web/HTTP/MethodsAny supported HTTP method
headersObjecttrue{}{ Authorization: 'Basic 1234' }The headers required to perform the actual HTTP request

Developing and testing locally

First, install the package dependencies for this workspace at the root of the repo, then run yarn install or npm install in this folder.

Files:

  • Command: packages/cypress-mock-openapi/src/index.js
  • Plugin: packages/cypress-mock-openapi/src/plugin.js
  • Mock server data: packages/cypress-mock-openapi/test/json/db.json
  • Spec: packages/cypress-mock-openapi/test/specs/mock.spec.js

Commands:

yarn test:open # Opens the Cypress UI
yarn test # Runs the tests in a headless browser

FAQs

Package last updated on 28 Nov 2022

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.