Skip to main content
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.
Prerequisites:

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:
  1. Spec loading fails - The adapter fetches the OpenAPI spec at startup
  2. API calls fail - Generated tools call the real API
  3. Tests aren’t isolated - External state affects test results
  4. Tests are slow - Network latency adds up

Step 1: Set Up Test Configuration

First, configure your test file:
openapi-app.e2e.spec.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.spec.ts

Best Practices

The OpenAPI spec is fetched when the server starts. Mock it in beforeEach:
Mock responses should match the schema defined in your OpenAPI spec:
Use call tracking to verify headers, body, and URL:
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