# swagger-test-templates

> Generate test code from a Swagger spec

Latest version **1.6.0** (published 2019-10-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install swagger-test-templates
pnpm add swagger-test-templates
yarn add swagger-test-templates
bun add swagger-test-templates
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.6.0 |
| Published | 2019-10-15 |
| First published | 2015-06-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 6 |
| Unpacked size | 477.9 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 168 |
| Maintainers | elsapeng, mohsen, noahdietz, swagger |
| Keywords | Swagger, API, Test |

## Links

- npm: https://www.npmjs.com/package/swagger-test-templates
- Repository: https://github.com/apigee-127/swagger-test-templates
- Homepage: https://github.com/apigee-127/swagger-test-templates#readme
- Issues: https://github.com/apigee-127/swagger-test-templates/issues
- npm.io page: https://npm.io/package/swagger-test-templates

## Dependencies (6)

- [lodash](https://npm.io/package/lodash.md) ^4.17.12
- [string](https://npm.io/package/string.md) ^3.3.0
- [handlebars](https://npm.io/package/handlebars.md) ^4.0.5
- [js-string-escape](https://npm.io/package/js-string-escape.md) ^1.0.1
- [sanitize-filename](https://npm.io/package/sanitize-filename.md) ^1.3.0
- [json-schema-deref-sync](https://npm.io/package/json-schema-deref-sync.md) ^0.6.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.6.0 (latest) — 2019-10-15
- 1.5.1 — 2018-11-07
- 1.5.0 — 2018-05-05
- 1.4.3 — 2017-09-06
- 1.4.2 — 2017-05-16
- 1.4.1 — 2017-05-09
- 1.4.0 — 2017-05-05
- 1.3.0 — 2016-10-13
- 1.2.0 — 2015-08-17
- 1.1.0 — 2015-07-27
- 0.1.1 — 2015-07-14
- 0.1.0 — 2015-07-14
- 0.0.0-alpha — 2015-06-23

## README

# Swagger Test Templates

| Linux | Windows |
| ----- | ------- |
| [![Build Status](https://travis-ci.org/apigee-127/swagger-test-templates.svg?branch=master)](https://travis-ci.org/apigee-127/swagger-test-templates) | [![Build Status](https://ci.appveyor.com/api/projects/status/github/apigee-127/swagger-test-templates?svg=true&branch=master)](https://ci.appveyor.com/project/noahdietz/swagger-test-templates) |

> Generate test code from a [Swagger](http://swagger.io) spec(version 2.0)

## Usage

Install via npm

```bash
npm install --save swagger-test-templates
```

Use your [Swagger](http://swagger.io) API spec file to generate test for your API.

```javascript
var stt = require('swagger-test-templates');
var swagger = require('/path/to/swagger.json');
var config = {
  assertionFormat: 'should',
  testModule: 'supertest',
  pathName: ['/user', '/user/{id}'],
  loadTest: [{pathName:'/user', operation:'get', load:{requests: 1000, concurrent: 100}}, { /* ... */ }],
  maxLen: 80,
  pathParams: {
    "id": "0123"
  }
};

// Generates an array of objects containing the test file content, following specified configuration
// the array contains objects with the scheme { name: <test-file-name>, test: <test-file-content> }
// tests = [ {name: base-path-test.js, test: ... }, {name: users-test.js, test: ... }]
var tests = stt.testGen(swagger, config);
```

## API

`swagger-test-templates` module exports a function with following arguments and return values:

### Arguments

* **`assertionFormat`** *required*: One of `should`, `expect` or `assert`. Choose which assertion method should be used in output test code.
* **`testModule`** *required*: One of `supertest` or `request`. Choose between direct API calls (`request`) vs. programatic access to your API (`supertest`).
* **`pathName`** *required*: List of path names available in your Swagger API spec used to generate tests for. Empty array leads to **all paths**.
* **`statusCodes`** *optional* Array with status codes to generate tests for. Useful for generating only happy-flow tests. Excluding this param will generate tests for all responses. A test is generated for the status code only when the status code is listed in the Swagger API spec.
* **`loadTest`** *optional*: List of objects info in your Swagger API spec used to generate stress tests. If specify, pathName & operation are **required**. Optional fields requests defaults to `1000`, concurrent defaults to `100`.
* **`maxLen`** *optional*: Maximum line length. If set to `-1`, descriptions will not be truncated. Defaults to `80`.
* **`pathParams`** *optional*: Object containing the values of a specific path parameters.
* **`templatesPath`** *optional* String indicating a custom handlebars-template path for generating the tests. Note: copy all the templates to your custom directory, this is a 'all-or-nothing' path
* **`requestData`** *optional* Object containing data to send with the request See section on requestData for more details

### Return value

An array in which each string is content of a test file and the file name. Use this information to write those files to disk.

## Sending requestData

Based on your schema there are a few modules out there that allow you to generate mock request payloads.
You can send this mock data along with the tests generated by this module by filling the `requestData` property of the module.
The mock data needs to have the following structure:

### Mock HTTP request body

```javascript
{
   '/endpoint': {
       operation: {
           'responseCode': [{ body: {}, description:'some description of the data'}]
       }
   }
 }

```

### Mock Path Parameters

```javascript
{
   '/pet/{name}': {
       get: {
           '200': [{ name: 'spot', description:'some description of the data'}]
       }
   }
 }

```

### Mock Query Parameters

This will make a request to `/pet?name=spot` assuming that your swagger API has a definition for a `name` query parameter.

```javascript
{
   '/pet': {
       get: {
           '200': [{ name: 'spot', description:'some description of the data'}]
       }
   }
 }

```

### Mock HTTP Headers

This will add an HTTP header `X-Token` set to `waestrydtufj` assuming that your swagger API has a definition for that header.

```javascript
{
   '/pet': {
       get: {
           '200': [{ 'X-Token': 'waestrydtufj', description:'some description of the data'}]
       }
   }
 }

```

Multiple objects within a status code request data array are supported. So, for example this could be:

```javascript
{
     '/pet': {
         post: {
             '200': [
               {
                 body: {
                  id: 1,
                  otherProperty: 'some property that is a string'
                 },
                 description: 'the description for this data'
               },
               {
                 body: {
                  id: 2,
                  otherProperty: 'another value of that property'
                 },
                 description: 'the description for another data'
               }
             ]
         },
         get: {
            '200': [
              {
                guid: 'some_string_to_place_in_path',
                anotherPathParam: 100,
                description: 'valid path or query parameters'
              },
              {
                guid: 'some_other_string_to_place_in_path',
                anotherPathParam: 200,
                description: 'another valid path or query parameters'
              }
          ]
         }
     }
 }
```

Note: for get-requests matching data will be transferred to the pathParams. So setting config.pathParams directly will have the same effect (see above).

Every mockData item in the `responseCode` array will be used to generate a test. The description will be added to the "it" function for reference.

Parameters explicitly marked as `required: false` in your Swagger API spec, will only be set if there is a matching value in requestData object. Required parameters and parameters without explicitly set `required` flag in Swagger API spec will be set to either a matching value in requestData object or 'DATA GOES HERE' string.

## License

[MIT](/LICENSE)

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