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

@flowko/cypress-swagger-validation

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@flowko/cypress-swagger-validation

Validate your Swagger file and request responses against Swagger Endpoints

  • 0.2.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Cypress Swagger Validation Plugin

Validate your request responses against Swagger JSON Endpoints. AKA Contract Testing.

npm (scoped) npm (types) npm (licence)

Do you use Cypress to perform API endpoint testing? Do you have Swagger/Openapi v3 schema? This is the plugin for you.

See the example swagger files to see how the usage below works with it.

Your swagger doc will need endpoints with content schema defined.

Cypress Installation

yarn add @flowko/cypress-swagger-validation

Then in your cypress Plugins file:

const {SwaggerValidation} = require('@flowko/cypress-swagger-validation');

module.exports = (on, config) => {
    // ...
    on('task', SwaggerValidation(config));
    // ...
    return config;
};

Cypress Usage

describe('Basic API checks', () => {
    it('Should be a valid swagger schema', function () {
        cy.task('validateSwaggerFile', {
            file: './testing/swagger.json',  // optional path or full URL, see below
        }).should('equal', null);
    });

    it('Should return a valid health payload', function () {
        cy.request('/healthz').then($response => {
            // Check the swagger schema:
            cy.task('validateSwaggerSchema', {
                file:           './testing/swagger.json',  // optional path or full URL, see below
                endpoint:       '/healthz',
                method:         'get',
                statusCode:     200,
                responseSchema: $response.body,
                verbose:        true,                      // optional, default: false
            }).should('equal', null);
        });
    });
});

The swagger file

This can either be a file on disk or a URL.

When using a file on disk and due to the fact that this plugin runs on the Cypress Backend, the location of the file must be defined as either the full path on disk or relative path to the running of the cypress command.

You can define the swagger file location either with an environment variable which can apply to all tests:

config.env.swaggerFile

or within each individial test using the options below.

Options

validateSwaggerFile
OptionDescriptionOptionalDefault
fileThe location of the swagger file to use for contract testingtrueconfig.env.swaggerFile
validateSwaggerSchema
OptionDescriptionOptionalDefault
fileThe location of the swagger file to use for contract testingtrueconfig.env.swaggerFile
endpointThe name of the swagger endpoint to check
methodThe request method of the endpoint
statuscodeThe http status code beneath the method
responseSchemaThe payload of the API response to validate
verboseConsole.log more info when validation failstruefalse

Compiling Source

yarn install
yarn build
yarn test

Keywords

FAQs

Package last updated on 09 Feb 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