1.4.2 • Published 3 years ago

chai-http-swagger v1.4.2

Weekly downloads
3
License
ISC
Repository
-
Last release
3 years ago

Chai-HTTP-Swagger

A Chai plugin for generating Swagger documentation from Chai HTTP testcases

The library provides Chai assertions for responses from http requests made using Chai-HTTP and documents your api. This library automatically generates Swagger/OpenAPI documentation and schemas.


Installation

npm install chai-http-swagger

Summary


Usage

// app.js
const express = require("express");
const { setup } = require("chai-http-swagger");
const app = express();
const options = {
  swagger: {
    // ... Your swagger configurations
  },
  config: {
    swaggerPath: __dirname, // your .yamlfiles directory
    fileName: "test", // your custom fileName for generated .yaml files
  },
};
const chaiHttpSwaggerSetup = setup(options);

app.use("/swagger", chaiHttpSwaggerSetup.swagger);

app.get("/status", (req, res) => {
  res.sendStatus(200);
  res.json({
    msg: "HELLO WORLD",
  });
});

app.listen(3000);
// ... etc

// test.js
const chai = require("chai");
const chaiHttpSwagger = require("chai-http-swagger");
const app = require("./app");

chai.use(chaiHttpSwagger.httpClient);

it("should check status", (done) => {
  chai
    .request({
      app, // your express application
      method: "get", // request method
      path: "/status",
    })
    .end((err, res) => {
      res.should.have.status(200);
      res.should.be.a("Object");
      // your asserts
      done();
    });
});

Run your tests and generate .yaml files for Swagger


Send Request Body


You can send body application/json or multipart/form-data content types


JSON

Send json data to api

it("should create new data", (done) => {
  const data = {
    msg: "Hello World",
  };
  chai
    .request({
      app,
      method: "post",
      path: "/data",
      body: {
        json: data, // this is automatically set Content-Type: application/json
      },
    })
    // .send(data)
    .end((err, res) => {
      res.should.have.status(200);
      res.should.be.a("Object");
      targetUser = res.body;
      done();
    });
});

FormData

Send form-data to api

it("should create new data", (done) => {
  const data = {
    msg: "Hello World",
  };
  chai
    .request({
      app,
      method: "post",
      path: "/data",
      body: {
        formData: data, // this is automatically set Content-Type: multipart/form-data
      },
    })
    // .send(data)
    .end((err, res) => {
      res.should.have.status(200);
      res.should.be.a("Object");
      targetUser = res.body;
      done();
    });
});

Upload file

it("should create new data", (done) => {
  const data = {
    msg: "Hello World",
  };
  chai
    .request({
      app,
      method: "post",
      path: "/data",
      body: {
        formData: {
          ...data,
          attachments: {
            // files
            imgFile: [`/img.png`, "img.png"],
          },
        }, // this is automatically set Content-Type: multipart/form-data
      },
    })
    // .send(data)
    .end((err, res) => {
      res.should.have.status(200);
      res.should.be.a("Object");
      targetUser = res.body;
      done();
    });
});

You can send file to server in the attachments


Secured apis

Add Security schema to your swagger config

const options = {
  swagger: {
    swaggerDefinition: {
      openapi: "3.0.0",
      components: {
        securitySchemes: {
          Bearer: {
            type: "apiKey",
            name: "Authorization", // api security key name
            in: "header",
            description: "Please use login api to get accessToken",
          },
        },
      },
    },
  },
  config: {},
};

After configuring Swagger, add security property in your request

it("should get private message", (done) => {
  chai
    .request({
      app,
      method: "get",
      path: "/private",
      security: {
        Authorization: "token", // your token data
      },
    })
    .end((err, res) => {
      res.should.have.status(200);
      res.should.be.a("Object");
      res.body.should.be.a("Object");
      res.body.msg.should.be.equal("PRIVATE_MESSAGE");
      done();
    });
});

If you want more examples, see this


Demo

Demo


Happy Coding!

1.3.7

3 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.9

3 years ago

1.3.8

3 years ago

1.3.6

3 years ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.8

3 years ago

1.2.9

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago