Mocking Strategies
Mocking Strategies
The testing-api serves as a centralized source of truth for data structures across the Supervised AI platform. By utilizing these mocking strategies, developers can decouple frontend and backend development cycles and ensure service-to-service reliability without requiring live dependencies.
Frontend Data Simulation
For frontend development, the testing-api allows you to generate realistic JSON responses that match the production schemas. This ensures that UI components are built against accurate data structures.
Dynamic Mock Generation
Use the standard generator interface to retrieve randomized but schema-compliant data.
import { MockFactory } from '@supervised-ai/testing-api';
// Generate a mock response for a Dataset object
const mockDataset = MockFactory.create('Dataset', {
count: 5,
overrides: {
status: 'processing'
}
});
Mock Service Worker (MSW) Integration
To intercept network requests in a browser or test environment, integrate the testing-api with MSW.
import { rest } from 'msw';
import { SchemaMocks } from '@supervised-ai/testing-api';
export const handlers = [
rest.get('/api/v1/models/:id', (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json(SchemaMocks.getModel({ id: req.params.id }))
);
}),
];
Service-to-Service Mocking
In a microservices architecture, testing a service that depends on another can be challenging. The testing-api provides contract-based mocks to simulate downstream responses.
Proxying to the Mock Server
Instead of hitting a live service during integration tests, point your service's environment variables to the testing-api mock endpoint.
| Environment Variable | Value |
| :--- | :--- |
| INFERENCE_SERVICE_URL | http://localhost:4000/mock/inference |
| DATA_CLEANING_URL | http://localhost:4000/mock/cleaning |
Validating Requests
The testing-api can also act as a spy to ensure your service is sending the correct payloads to its neighbors.
# Example: Verify if the correct payload was sent to the mock endpoint
GET http://localhost:4000/inspect/last-request?service=inference
Advanced Mock Configuration
Seeding for Deterministic Tests
By default, mocks are randomized. For regression testing or snapshot testing, use a seed to ensure the output remains consistent across runs.
// All subsequent calls will produce the same data for the given seed
testingApi.setSeed(12345);
const data1 = MockFactory.create('User');
const data2 = MockFactory.create('User'); // Identical to data1
Partial Overrides
When testing specific edge cases (e.g., an empty list or a failed status), use the overrides parameter to inject specific values into the generated mock while keeping the rest of the object schema-valid.
| Parameter | Type | Description |
| :--- | :--- | :--- |
| schema | string | The name of the model/interface to mock. |
| count | number | Number of items to generate (default: 1). |
| overrides | Object | Key-value pairs to force specific data points. |
const errorMock = MockFactory.create('JobResponse', {
overrides: {
exit_code: 1,
stack_trace: 'NullPointerException at line 42'
}
});