# mocha-swagger-tests

> Automatic validation of Swagger schemas and responses

Latest version **1.0.0** (published 2015-03-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install mocha-swagger-tests
pnpm add mocha-swagger-tests
yarn add mocha-swagger-tests
bun add mocha-swagger-tests
```

## Health

**Score 5/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: insecure dependencies; abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2015-03-01 |
| First published | 2015-03-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Matt Apperson |
| Maintainers | hatchback |
| Keywords | swagger, mocha, test, assert |

## Links

- npm: https://www.npmjs.com/package/mocha-swagger-tests
- Repository: https://github.com/hatchback/swagger-mocha
- Issues: https://github.com/hatchback/swagger-mocha/issues
- npm.io page: https://npm.io/package/mocha-swagger-tests

## Dependencies (5)

- [tv4](https://npm.io/package/tv4.md) git://github.com/ahultgren/tv4
- [async](https://npm.io/package/async.md) ^0.9.0
- [mocha](https://npm.io/package/mocha.md) ^2.0.1
- [expect.js](https://npm.io/package/expect.js.md) ^0.3.1
- [supertest](https://npm.io/package/supertest.md) ^0.14.0

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2015-03-01

## README

# mocha Swagger tests

Automatic validation of Swagger paths schemas and responses.
Using mocha for pretty error outputting.

Supports swagger 1.2 only. Should support any REST api

## Usage

```js
var SwaggerTest = require('mocha-swagger-tests');
var app = require('../path/to/something/that/exposes/an/http/server');

var swaggerTest = new SwaggerTest({swaggerPath: '/docs.json'});

// - Optional
swaggerTest.customTest({
    requestUrl: '/resource?test-with-query-parameter=1337',
    path: '/resource',
    method: 'get',
    status: 200,
    customTest: function (result) {
      it('should be leet', function () {
        expect(result).to.be('1337');
      });
    }
});

// - Optional
swaggerTest.before = function(done) {
    // Do something to start the server, does not matter if sync or not
    swaggerTest.setApp(app);
    done();
};

// - Optional
swaggerTest.validParams = {
  resourceId: '1337'
};

// - NOT Optional
swaggerTest.run();
```

## API

### `new SwaggerTest(config)`

* `swaggerPath`, the path to the swagger.json file. Default is /swagger.json

### `SwaggerTest#customTest = function(request)`

* `request`
  * `requestUrl`, the path (plus any query strings) to test
  * `path`, the name of the specs for the path in the swagger.json file
  * `method`, which method to use (e.g. "get" or "post")
  * `status`, which status to expect. Can be used to test error responses too
  * [`customTest`], additional testing of the result, other than the automatic schema validation (e.g. the number of items etc)

### `SwaggerTest#validParams = {}`

* `validParams`, a key-value map of valid values for path parameters. When
  encountering a path like /resource/{id}, a property with the same name is
  expected to be found in `validParams`. This implementation is admittedly a bit
  fragile.

### `SwaggerTest#run(callback)`

Prepare the tests.

### `SwaggerTest.run(callback)`

Run mocha.

## Todo

* Tests

---
_Source: https://npm.io/package/mocha-swagger-tests · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
