|
| 1 | +const { SnapshotAgent, setGlobalDispatcher, getGlobalDispatcher, request } = require('../../index.js') |
| 2 | +const { createServer } = require('node:http') |
| 3 | +const { promisify } = require('node:util') |
| 4 | +const { tmpdir } = require('node:os') |
| 5 | +const { join } = require('node:path') |
| 6 | + |
| 7 | +/** |
| 8 | + * Example: Basic Snapshot Testing |
| 9 | + * |
| 10 | + * This example demonstrates how to use SnapshotAgent to record API |
| 11 | + * interactions and replay them in tests for consistent, offline testing. |
| 12 | + */ |
| 13 | + |
| 14 | +async function basicSnapshotExample () { |
| 15 | + console.log('🚀 Basic Snapshot Testing Example\n') |
| 16 | + |
| 17 | + // Create a temporary snapshot file path |
| 18 | + const snapshotPath = join(tmpdir(), `snapshot-example-${Date.now()}.json`) |
| 19 | + console.log(`📁 Using temporary snapshot file: ${snapshotPath}\n`) |
| 20 | + |
| 21 | + // Create a local test server |
| 22 | + const server = createServer((req, res) => { |
| 23 | + res.writeHead(200, { 'Content-Type': 'application/json' }) |
| 24 | + res.end(JSON.stringify({ |
| 25 | + message: 'Hello from test server!', |
| 26 | + timestamp: new Date().toISOString(), |
| 27 | + path: req.url |
| 28 | + })) |
| 29 | + }) |
| 30 | + |
| 31 | + await promisify(server.listen.bind(server))(0) |
| 32 | + const { port } = server.address() |
| 33 | + const origin = `http://localhost:${port}` |
| 34 | + |
| 35 | + try { |
| 36 | + // Step 1: Record mode - capture API responses |
| 37 | + console.log('📹 Step 1: Recording API response...') |
| 38 | + |
| 39 | + const recordingAgent = new SnapshotAgent({ |
| 40 | + mode: 'record', |
| 41 | + snapshotPath |
| 42 | + }) |
| 43 | + |
| 44 | + const originalDispatcher = getGlobalDispatcher() |
| 45 | + setGlobalDispatcher(recordingAgent) |
| 46 | + |
| 47 | + try { |
| 48 | + // Make an API call that will be recorded |
| 49 | + const response = await request(`${origin}/api/test`) |
| 50 | + const data = await response.body.json() |
| 51 | + |
| 52 | + console.log(`✅ Recorded response: ${data.message}`) |
| 53 | + |
| 54 | + // Save the recorded snapshots |
| 55 | + await recordingAgent.saveSnapshots() |
| 56 | + console.log('💾 Snapshot saved to temporary file\n') |
| 57 | + } finally { |
| 58 | + setGlobalDispatcher(originalDispatcher) |
| 59 | + recordingAgent.close() |
| 60 | + } |
| 61 | + |
| 62 | + // Step 2: Playback mode - use recorded responses (server can be down) |
| 63 | + console.log('🎬 Step 2: Playing back recorded response...') |
| 64 | + server.close() // Close server to prove we're using snapshots |
| 65 | + |
| 66 | + const playbackAgent = new SnapshotAgent({ |
| 67 | + mode: 'playback', |
| 68 | + snapshotPath |
| 69 | + }) |
| 70 | + |
| 71 | + setGlobalDispatcher(playbackAgent) |
| 72 | + |
| 73 | + try { |
| 74 | + // This will use the recorded response instead of making a real request |
| 75 | + const response = await request(`${origin}/api/test`) |
| 76 | + const data = await response.body.json() |
| 77 | + |
| 78 | + console.log(`✅ Playback response: ${data.message}`) |
| 79 | + console.log('🎉 Successfully used recorded data instead of live server!') |
| 80 | + } finally { |
| 81 | + setGlobalDispatcher(originalDispatcher) |
| 82 | + playbackAgent.close() |
| 83 | + } |
| 84 | + } finally { |
| 85 | + // Ensure server is closed |
| 86 | + if (server.listening) { |
| 87 | + server.close() |
| 88 | + } |
| 89 | + |
| 90 | + // Clean up temporary file |
| 91 | + try { |
| 92 | + const { unlink } = require('node:fs/promises') |
| 93 | + await unlink(snapshotPath) |
| 94 | + console.log('\n🗑️ Cleaned up temporary snapshot file') |
| 95 | + } catch (error) { |
| 96 | + // File might not exist or already be deleted |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// Main execution |
| 102 | +async function main () { |
| 103 | + await basicSnapshotExample() |
| 104 | +} |
| 105 | + |
| 106 | +// Run if called directly |
| 107 | +if (require.main === module) { |
| 108 | + main().catch(console.error) |
| 109 | +} |
0 commit comments