Software Testing & QA (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
API Testing with Schema Validation (Zod / JSON Schema).
senior LevelCode ExampleFormsTestingSystem Design
In Simple Words: Testing REST APIs should verify both HTTP status codes and contract fidelity:
typescript
import { z } from 'zod';
const UserResponseSchema = z.object({
id: z.string().uuid(),
email: z.string().email(),
roles: z.array(z.enum(['admin', 'user'])),
createdAt: z.string().datetime()
});
test('GET /api/users/me returns valid user contract', async () => {
const res = await request(app).get('/api/users/me').set('Authorization', token);
expect(res.status).toBe(200);
// Validates exact data types, nullable states, and field shapes
const parseResult = UserResponseSchema.safeParse(res.body);
expect(parseResult.success).toBe(true);
});