A demo of API automation
- API methods:
GET,POST, andDELETE(cleanup) - Request complexity: path params and nested JSON request bodies
- Response complexity: nested objects + arrays
- Advanced verification:
- Typed schema validation using
zod(deserialization-style checks) - Full-response snapshot verification (approval testing style)
- Typed schema validation using
- Builder pattern for reusable and maintainable test data setup
- Negative-path checks for invalid payloads and missing resources
- Mocking strategy using
nockfor deterministic API tests
- TypeScript
- Vitest
- Axios
- Zod
- Nock
This is a conceptual view of how everything hangs together
flowchart LR
A["Vitest (test runner)"] --> B["Test Files<br/>mock + live"]
B --> C["PetstoreClient"]
C --> D["Axios (HTTP client)"]
C --> E["Zod (request/response validation)"]
subgraph Mocked Path
B --> M["Nock intercepts HTTP"]
M --> D
D --> N["No real network call"]
end
subgraph Live Path
D --> P["Swagger Petstore API"]
end
E --> Q["Typed, validated data<br/>for assertions"]
A --> R["Assertions + snapshots + reports"]
Q --> R
Vitestruns the suite and performs assertions/snapshot checks.PetstoreClientis the thin abstraction your tests call.Axiosperforms HTTP requests.Zodvalidates request and response payloads at runtime while keeping TypeScript types aligned.Nockis used only for mock tests to intercept HTTP and avoid external dependencies.
src/
client/
petstoreClient.ts
config/
env.ts
models/
pet.ts
tests/
builders/
petBuilder.ts
live/
petstore.live.test.ts
setup.ts
mock/
petstore.mock.test.ts
setup.ts
.github/workflows/
ci.yml
- Install dependencies
npm install- Run deterministic mocked tests (recommended default)
npm run test:mock- Run live Petstore tests
npm run test:liveCopy .env.example to .env if you want to override the default API base URL.
PETSTORE_BASE_URL=https://petstore.swagger.io/v2npm run test:mockruns only mock tests to keep CI stable and fast.- Live tests are intentionally separate because public API data/state can be unstable.