Snapshot testing is a powerful feature that captures the output of your MCP server operations and compares them against saved snapshots in future test runs. This helps you detect unexpected changes in your server's behavior and ensures consistency across updates.
Snapshot testing works by:
- Capturing the output of a tool, resource, or prompt on the first run
- Saving this output as a "snapshot" file
- Comparing future outputs against this saved snapshot
- Alerting you when the output changes
This is particularly useful for:
- Detecting breaking changes in API responses
- Ensuring consistent formatting of outputs
- Regression testing complex data structures
- Documentation through examples
await mcpTest(serverConfig, {
tools: {
search: {
args: { query: 'test' },
snapshot: true // Enable snapshot testing
}
}
});On the first run, this creates a snapshot file. On subsequent runs, it compares the output against the saved snapshot.
For better organization, you can provide custom names:
await mcpTest(serverConfig, {
tools: {
calculatePrice: {
args: { items: ['apple', 'banana'], tax: 0.08 },
snapshot: 'price-calculation-with-tax'
}
}
});Sometimes you only want to snapshot specific parts of the response:
await mcpTest(serverConfig, {
tools: {
getUserInfo: {
args: { userId: '123' },
snapshot: {
properties: [
'content[0].type',
'content[0].text',
'metadata.version'
]
}
}
}
});Exclude fields that change on every run (timestamps, IDs, etc.):
await mcpTest(serverConfig, {
resources: {
'status.json': {
snapshot: {
exclude: ['timestamp', 'requestId', 'uptime']
}
}
}
});Combine multiple options for precise control:
await mcpTest(serverConfig, {
tools: {
generateReport: {
args: { type: 'monthly' },
snapshot: {
name: 'monthly-report-structure',
properties: ['content', 'summary', 'totals'],
exclude: ['generatedAt', 'processingTimeMs']
}
}
}
});When you intentionally change your server's output, you'll need to update the snapshots.
# Update all snapshots
mcp-jest server.js --tools search --update-snapshots
# Or use the short flag
mcp-jest server.js --tools search -uUPDATE_SNAPSHOTS=true npm testprocess.env.UPDATE_SNAPSHOTS = 'true';
await mcpTest(serverConfig, testConfig);Snapshots are stored in a __snapshots__ directory relative to your current working directory:
project/
├── __snapshots__/
│ ├── tool-search-a1b2c3d4.snap.json
│ ├── resource-config_json-e5f6g7h8.snap.json
│ └── prompt-codeReview-i9j0k1l2.snap.json
├── server.js
└── test.js
Each snapshot file contains:
- The captured data
- Metadata about the test (type, name, server command)
- Timestamp of creation
When a snapshot doesn't match, you'll see a detailed diff:
❌ Tool 'search' snapshot
Snapshot mismatch:
- results.count: 5
+ results.count: 6
+ results[5]: {"id": "new-item", "title": "New Result"}
This shows:
-Lines that were in the snapshot but not in current output+Lines that are in current output but not in the snapshot
Always commit snapshot files to version control. They're part of your test suite and help track changes over time.
Before running --update-snapshots, carefully review the differences to ensure they're intentional.
For complex tests, use descriptive snapshot names:
snapshot: 'user-profile-with-premium-features'
// instead of
snapshot: trueAlways exclude data that changes between runs:
snapshot: {
exclude: [
'timestamp',
'requestId',
'sessionId',
'randomValue',
'processingTime'
]
}For API stability, often the structure matters more than exact values:
snapshot: {
properties: [
'data.users[0]', // Just verify first user exists
'data.pagination', // Verify pagination structure
'metadata.version' // Track API version
]
}// Ensure math operations remain consistent
await mcpTest(serverConfig, {
tools: {
calculate: {
args: { expression: '(10 + 5) * 2' },
snapshot: 'basic-arithmetic'
}
}
});// Verify configuration structure doesn't change
await mcpTest(serverConfig, {
resources: {
'config.json': {
snapshot: {
name: 'app-configuration',
exclude: ['lastModified', 'buildNumber']
}
}
}
});// Test AI-generated content structure
await mcpTest(serverConfig, {
prompts: {
codeReview: {
args: {
code: 'function fibonacci(n) { return n <= 1 ? n : fibonacci(n-1) + fibonacci(n-2); }'
},
snapshot: {
name: 'code-review-fibonacci',
properties: [
'content[0].type', // Verify response type
'suggestions.length', // Number of suggestions
'overallAssessment.score' // Numerical score exists
],
exclude: [
'reviewId',
'reviewedAt',
'content[0].text' // Exact wording may vary
]
}
}
}
});- Ensure you have write permissions in the current directory
- Check that
UPDATE_SNAPSHOTS=trueis set on first run - Verify the snapshot configuration is correct
- Check if you're excluding all dynamic fields
- Ensure your server returns consistent results for the same inputs
- Verify the snapshot files aren't corrupted
- Consider using
propertiesto snapshot only essential parts - Exclude large binary data or generated content
- Use multiple focused snapshots instead of one large one
If you're currently using explicit assertions, snapshot testing can simplify your tests:
// Before: Manual assertions
{
tools: {
search: {
args: { query: 'test' },
expect: (result) => {
return result.content?.length > 0 &&
result.content[0].type === 'text' &&
result.metadata?.count > 0;
}
}
}
}
// After: Snapshot testing
{
tools: {
search: {
args: { query: 'test' },
snapshot: {
exclude: ['timestamp', 'metadata.count']
}
}
}
}The snapshot approach is more maintainable and provides better error messages when things change.