Core Concepts
Understanding the fundamental concepts behind MockMaster.
MockMaster is built around several core concepts that work together to provide type-safe API mocking with record and replay capabilities. This section explains each concept in detail.
Main Concepts
Record & Replay
The record and replay pattern is at the heart of MockMaster. Learn how to capture real API responses and replay them deterministically in your tests.
Key topics:
- Creating recordings from API responses
- Building replay handlers
- Request matching and response generation
Scenarios & Recordings
A scenario is a named collection of recorded API interactions. A recording captures a single HTTP request and its corresponding response.
Key topics:
- Creating and managing scenarios
- Adding recordings to scenarios
- Storing scenarios as JSON files
- Version control best practices
Factory System
MockMaster’s factory system provides a powerful way to generate realistic, customizable test data using Faker.js.
Key topics:
- Defining factories for your data models
- Building single objects and lists
- Overriding default values
- Using sequences for unique IDs
OpenAPI Integration
MockMaster can automatically generate realistic mock data from OpenAPI 3.0 specifications.
Key topics:
- Parsing JSON and YAML specs
- Generating scenarios from schemas
- Extracting operations
- Working with complex schemas
Path Matching
MockMaster’s core package provides powerful path matching capabilities for HTTP routing.
Key topics:
- Exact path matching
- Path parameters (e.g.,
/users/:id) - Wildcard matching (e.g.,
/api/*) - Parameter extraction
How They Work Together
Here’s how these concepts work together in a typical workflow:
// 1. Parse OpenAPI spec (OpenAPI Integration)
import { parseYaml } from '@mockmaster/openapi'
const spec = parseYaml(`...`)
// 2. Generate scenario with recordings (Scenarios & Recordings)
import { generateScenariosFromSpec } from '@mockmaster/cli'
const scenarios = generateScenariosFromSpec(spec, 'user-api')
// 3. Create replay handler (Record & Replay)
import { createReplayHandler } from '@mockmaster/msw-adapter'
const handler = createReplayHandler(scenarios[0])
// 4. Match paths and replay responses (Path Matching)
const response = handler({ method: 'GET', path: '/users/123' })
// 5. Optionally generate custom data with factories (Factory System)
import { defineFactory, build, fake } from '@mockmaster/data'
const userFactory = defineFactory('user', {
id: (ctx) => ctx.sequence('user'),
name: () => fake.person.fullName()
})
const customUser = build(userFactory)Next Steps
Dive deeper into each concept: