This library runs Swagger/OpenAPI tests against a live endpoint. It verifies that the request can be made, and that the response is in the right format.
- Install
openapi-test
to your devDependencies:
npm install openapi-test -D
- Author tests per method in your Swagger/OpenAPI JSON file under the
"x-tests"
extension:
{
[...]
"paths": {
"/api/films": {
"get": {
"operationId": "/api/films/get",
"description": "Get all films.",
"responses": {
"200": {
"description": "An array of films.",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/film"
}
}
}
},
"x-tests": [
{
"description": "Get all films (success)",
"response": {
"status": 200
}
}
]
}
},
[...]
}
}
- Run
openapi-test
:
{
"scripts": {
"test": "openapi-test --file ./path/to/swagger.json"
}
}
--file
- the path to your Swagger/OpenAPI JSON file--host
(optional) - the hostname to test against (default: the"host"
specified in the Swagger JSON file)--token
(optional) - the JWT to authorize with--verbose
(optional) - whether to display the response body content (default:false
)
description
- The description of the test, used for reporting the test resultsauth
- Whether to add theOPENAPI_TOKEN
JWT to the header and authorize the request (default:false
)skip
- Iftrue
, this test will be skipped. (default:false
)params
(optional) - Supplies dynamic parameter values (e.g.,"name": ...
would supply the value for/{name}
in the path). Only needed for tests with URL params.request
- An object that contains:query
(optional) - A mapping of query parameter keys to their values (e.g.,"format": "json"
will append?format=json
to the URL)
response
- An object that contains:status
- A status code (e.g.,200
) or array of status codes (e.g.,[200, 301]
) that the expected response should match