When you use the OpenAPI Adapter to generate MCP tools from an API specification, you need to mock both the OpenAPI spec fetch and the actual API calls. This guide shows you how to write comprehensive tests for OpenAPI-generated tools.Documentation Index
Fetch the complete documentation index at: https://docs.agentfront.dev/llms.txt
Use this file to discover all available pages before exploring further.
Prerequisites:
- Understanding of OpenAPI Adapter
- Familiarity with @frontmcp/testing
- Basic knowledge of HTTP Mocking
What You’ll Learn
- Mock OpenAPI spec loading
- Test generated CRUD operations
- Handle authentication in tests
- Test error scenarios
- Verify request/response transformations
The Challenge
OpenAPI-generated tools make HTTP requests to external APIs. Without mocking:- Spec loading fails - The adapter fetches the OpenAPI spec at startup
- API calls fail - Generated tools call the real API
- Tests aren’t isolated - External state affects test results
- Tests are slow - Network latency adds up
Step 1: Set Up Test Configuration
First, configure your test file:openapi-app.e2e.ts
Step 2: Mock the OpenAPI Specification
The adapter fetches the OpenAPI spec when the server starts. Mock this first:Step 3: Test Tool Discovery
Verify that tools are generated from the spec:Step 4: Test GET Operations
Test list and get-by-ID operations:Step 5: Test POST Operations
Test create operations with request body validation:Step 6: Test DELETE Operations
Test delete operations:Step 7: Test with Authentication
If your OpenAPI adapter uses authentication:Step 8: Test Error Scenarios
Cover various API error responses:Step 9: Test Query Parameters
Test operations with query parameters:Complete Test Suite Example
Here’s a complete, runnable test file:expense-api.e2e.ts
Best Practices
Always mock the spec endpoint
Always mock the spec endpoint
The OpenAPI spec is fetched when the server starts. Mock it in
beforeEach:Use realistic mock data
Use realistic mock data
Mock responses should match the schema defined in your OpenAPI spec:
Verify request details
Verify request details
Use call tracking to verify headers, body, and URL:
Test all error codes
Test all error codes
Cover 4xx and 5xx responses:
- 400 Bad Request (validation errors)
- 401 Unauthorized
- 403 Forbidden
- 404 Not Found
- 429 Rate Limited
- 500 Server Error
Next Steps
HTTP Mocking Reference
Complete HTTP mocking API
OpenAPI Adapter
Full adapter configuration
Testing Overview
Testing framework basics
Test Authentication
Testing with auth tokens