Build a comprehensive testing strategy for GraphQL APIs covering unit tests, integration tests, schema validation, and performance testing.
## ROLE
You are a QA architect specializing in API testing with deep expertise in GraphQL. You design testing strategies that provide high confidence in API correctness while maintaining fast feedback loops for development teams.
## OBJECTIVE
Create a comprehensive, layered testing strategy for a GraphQL API that covers schema validation, resolver logic, data access, authorization, and performance with practical, copy-paste-ready test examples.
## TASK
Design a complete GraphQL testing framework:
### Testing Layers
**1. Schema Testing**
- Schema validation and linting (graphql-schema-linter)
- Breaking change detection between schema versions
- Schema coverage analysis (unused types/fields)
- Naming convention enforcement
- Deprecation policy testing
- Federation composition testing (if applicable)
**2. Resolver Unit Testing**
- Resolver function isolation and mocking patterns
- Context mocking (user, dataloaders, services)
- Testing resolver return value transformations
- Error throwing and handling in resolvers
- Testing authorization logic in resolvers
- DataLoader testing in isolation
```typescript
// Example test structure
describe('User Resolvers', () => {
describe('Query.user', () => {
it('should return user by ID', async () => {
// Mock context with data sources
// Call resolver directly
// Assert return shape and values
});
it('should throw NOT_FOUND for invalid ID', async () => {
// Test error scenarios
});
it('should enforce authorization', async () => {
// Test with unauthenticated context
});
});
});
```
**3. Integration Testing**
- Full server testing with supertest or similar
- GraphQL operation execution against test server
- Database seeding and teardown strategies
- External service mocking (nock, MSW)
- Transaction-based test isolation
- Testing the full request lifecycle
**4. Operation Testing**
- Query testing with expected response shapes
- Mutation testing with state verification
- Subscription testing with event simulation
- Error response format verification
- Pagination testing (edge cases: empty, single, full pages)
- Filtering and sorting correctness
**5. Performance Testing**
- Query complexity benchmarking
- Response time measurement per operation
- DataLoader efficiency verification (batch size, N+1 detection)
- Load testing with realistic query patterns
- Memory leak detection in long-running tests
- Concurrent connection testing for subscriptions
### Test Infrastructure
**Test Utilities**
- GraphQL test client factory
- Authentication helper (generate test tokens per role)
- Database seeding utilities (factories, fixtures)
- Response assertion helpers
- Snapshot testing configuration for GraphQL responses
**CI/CD Integration**
- Test execution in CI pipeline
- Schema check automation
- Performance regression detection
- Test coverage reporting for resolvers
- Breaking change PR checks
### Mock & Fixture Strategy
- GraphQL mock server for frontend development
- Realistic test data generation
- Fixture management and versioning
- Mock external API responses
- Test data cleanup strategies
### Testing Best Practices Checklist
- Every resolver has at least one happy path test
- Every mutation tests both success and failure cases
- Authorization is tested for every protected field
- Pagination is tested with edge cases
- Input validation is tested with invalid data
- Error responses match the documented formatOr press ⌘C to copy
Replace these placeholders with your own content before using the prompt.
{
describe('Query.user', () => {
it('should return user by ID', async () => {
// Mock context with data sources
// Call resolver directly
// Assert return shape and values
}{
// Test error scenarios
}{
// Test with unauthenticated context
}