Giter VIP home page Giter VIP logo

cypress-swagger-validation's Introduction

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 @jc21/cypress-swagger-validation

Then in your cypress Plugins file:

const {SwaggerValidation} = require('@jc21/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

Option Description Optional Default
file The location of the swagger file to use for contract testing true config.env.swaggerFile

validateSwaggerSchema

Option Description Optional Default
file The location of the swagger file to use for contract testing true config.env.swaggerFile
endpoint The name of the swagger endpoint to check
method The request method of the endpoint
statuscode The http status code beneath the method
responseSchema The payload of the API response to validate
verbose Console.log more info when validation fails true false

Compiling Source

yarn install
yarn build
yarn test

cypress-swagger-validation's People

Contributors

jc21 avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.