mockforge-test
Test utilities for MockForge - easy integration with test frameworks like Playwright, Vitest, and any Rust test framework.
Features
- ๐ Easy Server Spawning: Start and stop MockForge servers programmatically
- โ Health Checks: Wait for server readiness with configurable timeouts
- ๐ Scenario Management: Switch scenarios/workspaces per-test
- ๐งน Automatic Cleanup: Processes are automatically cleaned up when dropped
- โ๏ธ Profile Support: Run with different MockForge profiles
- ๐ Protocol Support: Configure HTTP, WebSocket, gRPC, and admin endpoints
Installation
Add this to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["macros", "rt-multi-thread"] }
Quick Start
Basic Usage
use MockForgeServer;
async
With Specific Port
use MockForgeServer;
async
With OpenAPI Spec
use MockForgeServer;
async
Scenario Management
Switch between different test scenarios on the fly:
use MockForgeServer;
async
Loading Workspace from File
use MockForgeServer;
async
Dynamic Mock Updates
use MockForgeServer;
use json;
async
Advanced Configuration
Multiple Protocols
use MockForgeServer;
async
With Admin UI and Metrics
use MockForgeServer;
async
With Profile
use MockForgeServer;
async
Custom Binary Path
use MockForgeServer;
async
Server Management
Health Checks
use MockForgeServer;
async
Server Statistics
use MockForgeServer;
async
Reset Mocks
use MockForgeServer;
async
Integration Examples
With Playwright (Node.js/TypeScript)
While this is a Rust crate, you can use it to spawn MockForge servers for JavaScript/TypeScript tests:
- Create a Rust helper binary that uses
mockforge-test - Call it from your Playwright global setup
Example Rust helper:
// bin/test-server.rs
use MockForgeServer;
async
Then in your playwright.config.ts:
import { defineConfig } from '@playwright/test';
import { exec } from 'child_process';
export default defineConfig({
globalSetup: async () => {
// Start MockForge
const server = exec('cargo run --bin test-server');
// Wait for server to be ready
await new Promise(resolve => setTimeout(resolve, 2000));
return () => server.kill();
},
// ...
});
With Vitest
Similar to Playwright, create a Rust helper and call it from Vitest's globalSetup:
// vitest.config.ts
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
globalSetup: './test/setup.ts',
},
});
// test/setup.ts
import { exec } from 'child_process';
export async function setup() {
const server = exec('cargo run --bin test-server');
await new Promise(resolve => setTimeout(resolve, 2000));
return () => server.kill();
}
With Rust Test Frameworks
For native Rust testing, you can use the helper function:
use with_mockforge;
async
Configuration Reference
ServerConfig
| Field | Type | Default | Description |
|---|---|---|---|
http_port |
u16 |
0 (auto) |
HTTP server port |
ws_port |
Option<u16> |
None |
WebSocket server port |
grpc_port |
Option<u16> |
None |
gRPC server port |
admin_port |
Option<u16> |
None |
Admin UI port |
metrics_port |
Option<u16> |
None |
Metrics/Prometheus port |
spec_file |
Option<PathBuf> |
None |
OpenAPI spec file path |
workspace_dir |
Option<PathBuf> |
None |
Workspace directory |
profile |
Option<String> |
None |
Configuration profile |
enable_admin |
bool |
false |
Enable admin UI |
enable_metrics |
bool |
false |
Enable metrics endpoint |
extra_args |
Vec<String> |
[] |
Additional CLI arguments |
health_timeout |
Duration |
30s |
Health check timeout |
health_interval |
Duration |
100ms |
Health check interval |
working_dir |
Option<PathBuf> |
None |
Working directory |
env_vars |
Vec<(String, String)> |
[] |
Environment variables |
binary_path |
Option<PathBuf> |
None |
Path to mockforge binary |
Requirements
- MockForge CLI must be installed and available in PATH, or specify
binary_path - Install via:
cargo install mockforge-cli
Error Handling
All operations return Result<T, Error> where Error provides detailed information:
use ;
async
Logging
Enable logging to see detailed information:
// In your test setup
fmt
.with_env_filter
.init;
Or set the RUST_LOG environment variable:
RUST_LOG=mockforge_test=debug
Examples
See the examples directory for complete working examples.
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for details.
License
This project is licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.