Assertions API
Complete reference for assertion helpers.
assertResponse
Creates an assertion chain for an API response.
function assertResponse<T>(response: ApiResponse<T>): ResponseAssertion<T>
Example
import { assertResponse } from 'wdio-api-runner'
assertResponse(response)
.toBeSuccess()
.and.toHaveContentType('application/json')
Status Assertions
toHaveStatus
Asserts the response has a specific status code.
toHaveStatus(status: number): ResponseAssertion<T>
toBeSuccess / toBeOk
Asserts the response is successful (2xx).
toBeSuccess(): ResponseAssertion<T>
toBeOk(): ResponseAssertion<T>
toBeClientError
Asserts the response is a client error (4xx).
toBeClientError(): ResponseAssertion<T>
toBeServerError
Asserts the response is a server error (5xx).
toBeServerError(): ResponseAssertion<T>
Specific Status Methods
| Method | Status Code |
|---|---|
toBeCreated() | 201 |
toBeNoContent() | 204 |
toBeBadRequest() | 400 |
toBeUnauthorized() | 401 |
toBeForbidden() | 403 |
toBeNotFound() | 404 |
toBeConflict() | 409 |
toBeUnprocessableEntity() | 422 |
toBeInternalServerError() | 500 |
Header Assertions
toHaveHeader
Asserts the response has a header (optionally with value).
toHaveHeader(name: string, value?: string): ResponseAssertion<T>
toHaveContentType
Asserts the response has a specific content type.
toHaveContentType(contentType: string): ResponseAssertion<T>
Body Assertions
toHaveBodyProperty
Asserts the body has a property (optionally with value).
toHaveBodyProperty(path: string, value?: any): ResponseAssertion<T>
Supports nested paths:
assertResponse(response).toHaveBodyProperty('user.address.city', 'New York')
toHaveBodyContaining
Asserts the body contains a string.
toHaveBodyContaining(text: string): ResponseAssertion<T>
toHaveBodyMatching
Asserts the body matches a regex.
toHaveBodyMatching(pattern: RegExp): ResponseAssertion<T>
Performance Assertions
toRespondWithin
Asserts the response time is under a threshold.
toRespondWithin(ms: number): ResponseAssertion<T>
Schema Assertions
toMatchSchema
Asserts the body matches a JSON schema.
toMatchSchema(schema: object): ResponseAssertion<T>
Example
const schema = {
type: 'object',
required: ['id', 'name'],
properties: {
id: { type: 'number' },
name: { type: 'string' },
email: { type: 'string', format: 'email' }
}
}
assertResponse(response).toMatchSchema(schema)
Chaining
and
Chains assertions together.
assertResponse(response)
.toBeSuccess()
.and.toHaveContentType('application/json')
.and.toHaveBodyProperty('id')
.and.toRespondWithin(500)
Types
ResponseAssertion
interface ResponseAssertion<T> {
// Status
toHaveStatus(status: number): ResponseAssertion<T>
toBeSuccess(): ResponseAssertion<T>
toBeOk(): ResponseAssertion<T>
toBeClientError(): ResponseAssertion<T>
toBeServerError(): ResponseAssertion<T>
toBeCreated(): ResponseAssertion<T>
toBeNoContent(): ResponseAssertion<T>
toBeBadRequest(): ResponseAssertion<T>
toBeUnauthorized(): ResponseAssertion<T>
toBeForbidden(): ResponseAssertion<T>
toBeNotFound(): ResponseAssertion<T>
// Headers
toHaveHeader(name: string, value?: string): ResponseAssertion<T>
toHaveContentType(contentType: string): ResponseAssertion<T>
// Body
toHaveBodyProperty(path: string, value?: any): ResponseAssertion<T>
toHaveBodyContaining(text: string): ResponseAssertion<T>
toHaveBodyMatching(pattern: RegExp): ResponseAssertion<T>
// Schema
toMatchSchema(schema: object): ResponseAssertion<T>
// Performance
toRespondWithin(ms: number): ResponseAssertion<T>
// Chaining
and: ResponseAssertion<T>
}