Skip to content

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

License

MIT, MIT licenses found

Licenses found

MIT
LICENCE
MIT
LICENSE
Notifications You must be signed in to change notification settings

olahaida/cypress-swagger-validation

 
 

Repository files navigation

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 return a valid health payload', function () {
        cy.request('/healthz').then($response => {
            // Check the swagger schema:
            cy.task('validateSwaggerSchema', {
                file:           './testing/swagger.json',  // optional, see below
                endpoint:       '/healthz',
                method:         'get',
                statusCode:     200,
                responseSchema: $response.body,
                verbose:        true,                      // optional, default: false
            }).should('equal', null);
        });
    });
});

The swagger file

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

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

About

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

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENCE
MIT
LICENSE

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 98.9%
  • JavaScript 1.1%