Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
startHttpStub
✓ import { startHttpStub } from 'specmatic'
✗ const { startHttpStub } = require('specmatic')
Specmatic primarily uses named exports. Prefer ESM imports in modern Node.js environments.
test
✓ import { test } from 'specmatic'
✗ import test from 'specmatic'
The `test` function for running contract tests is a named export, not a default export.
setHttpStubExpectationJson
✓ import { setHttpStubExpectationJson } from 'specmatic'
✗ const setHttpStubExpectationJson = require('specmatic').setHttpStubExpectationJson
Use named imports for direct access to functions like `setHttpStubExpectationJson`.
Demonstrates how to programmatically start a Specmatic stub server, define and set dynamic expectations, simulate client interaction with the stub, run contract tests against it, and ensure the stub is properly shut down.
import { startHttpStub, setHttpStubExpectationJson, test, stopHttpStub } from 'specmatic';
import path from 'path'; // For resolving specmatic directory
// Define the directory where your Specmatic OpenAPI/contract specification files are located.
// For example, if your specs are in 'src/specmatic', use path.resolve(__dirname, 'src', 'specmatic')
const specmaticDir = path.resolve(__dirname, 'specmatic'); // Adjust this path as needed
async function runSpecmaticWorkflow() {
let stub;
try {
console.log(`Attempting to start Specmatic Stub server with specs from: ${specmaticDir}`);
// Start the Specmatic stub server, binding it to localhost:9000
// The specmaticDir is passed as an argument to load contract specifications.
stub = await startHttpStub('localhost', 9000, [specmaticDir]);
console.log(`Specmatic Stub running successfully on ${stub.url} (Process ID: ${stub.pid})`);
// Define and set a programmatic expectation for a GET /greeting endpoint on the stub.
// This allows the stub to respond with a specific JSON body for a matching request.
const expectation = {
"method": "GET",
"path": "/greeting",
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": {"message": "Hello from Specmatic Stub!"}
}
};
await setHttpStubExpectationJson(expectation, `http://localhost:${stub.port}`);
console.log('Stub expectation set for GET /greeting.');
// Simulate an API client interaction by fetching from the stub.
// In a real scenario, your application's HTTP client would call this.
console.log('Fetching from stub: GET /greeting');
const stubResponse = await fetch(`http://localhost:${stub.port}/greeting`);
const stubData = await stubResponse.json();
console.log('Received response from stub:', stubData);
// Run contract tests. This typically involves Specmatic verifying your API
// implementation against its OpenAPI specifications. For this example,
// we are testing against the running stub itself, which should pass if the spec is valid.
console.log(`Running contract tests against ${stub.url}`);
const testResult = await test(`http://localhost:${stub.port}`, [specmaticDir]);
console.log('Contract Test Results:', JSON.stringify(testResult, null, 2));
} catch (error) {
console.error('Specmatic workflow failed:', error);
process.exit(1);
} finally {
// Crucially, stop the Specmatic stub server to release resources and terminate the JAR process.
if (stub && stub.pid) {
console.log(`Stopping Specmatic Stub server (PID: ${stub.pid})...`);
await stopHttpStub(stub.pid);
console.log('Specmatic Stub stopped.');
}
}
}
// Execute the Specmatic workflow
runSpecmaticWorkflow();
specmatic --version
Errors
Common errors & fixes
Connection Refused error when connecting to stub
Node.js DNS resolution order preferring IPv6 on some systems, causing issues when Specmatic binds to IPv4.
fixSet `NODE_OPTIONS=--dns-result-order=ipv4first` in your shell or use `localhost` consistently instead of `127.0.0.1`.
ReferenceError: setImmediate is not defined
Execution environment lacks the Node.js global `setImmediate` function, often in browser-mocking test setups.
fixEnsure a standard Node.js test environment is used, or polyfill `setImmediate` for compatibility.
Audit
Dependencies
No dependency data recorded yet.