We have introduced support for auto-generated unit tests in the TypeScript SDKs using our OpenAPI test case extension.
Details
Unit tests are essential for ensuring the reliability and correctness of API calls. They help catch bugs early, simplify debugging, and improve the overall quality of the software. With auto-generated unit tests, developers can now save time and focus on more complex tasks while maintaining high code quality.
Unlike other SDKs, our TypeScript SDKs did not previously generate unit tests automatically. This required developers to manually write unit tests for each API call, which could be time-consuming and error-prone.
What's New?
With our latest update, to generate unit tests in TypeScript SDKs, you can either use our OpenAPI Test Cases Extension or simply provide examples for all responses, request parameters, and request bodies for any path in your OpenAPI specification.
The following OpenAPI specification contains two examples for response and request in the path /pet/findByStatus
.
- parameters > status > examples > example-1
- responses > 200 > examples > example-1
paths:
/pet/findByStatus:
get:
operationId: findPetsByStatus
parameters:
- name: status
in: query
description: Status values that need to be considered for filter
required: false
explode: true
schema:
$ref: '#/components/schemas/Status'
examples:
example-1:
summary: A sold status
value: sold
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
examples:
example-1:
summary: An example of a pet list response
value:
- photoUrls:
- "https://example.com/dog/side-pose"
- "https://example.com/dog/sitting"
name: "Dog"
- photoUrls:
- "https://example.com/cat/side-pose"
- "https://example.com/cat/sitting"
name: "Cat"
'400':
description: Invalid status value
Using these examples, APIMatic will automatically generate the corresponding unit test inside your TypeScript SDK with the following test
package structure.
The auto-generated unit test will not only make the API call but will also assert if the status code, headers, or the actual response body matches the expected ones in the provided examples. The unit test for the above sample OpenAPI would look like:
This unit test will be readily executable and will thoroughly test the path /pet/findByStatus
with the example data, providing success or failure status right in your IDE:
With this new feature, you can ensure your TypeScript SDKs are well-tested and reliable with minimal effort.