|
| 1 | +--- |
| 2 | +title: SnapshotPreviews Metadata |
| 3 | +sidebar_title: SnapshotPreviews Metadata |
| 4 | +sidebar_order: 4951 |
| 5 | +sidebar_section: features |
| 6 | +description: Add tags, context, and diff thresholds to SnapshotPreviews sidecar metadata. |
| 7 | +early_access: true |
| 8 | +--- |
| 9 | + |
| 10 | +<Include name="feature-available-for-user-group-early-adopter" /> |
| 11 | + |
| 12 | +SnapshotPreviews writes a JSON metadata sidecar next to each exported image when `TEST_RUNNER_SNAPSHOTS_EXPORT_DIR` is set. Sentry uploads this metadata with the image and uses it to group snapshots, apply local diff thresholds, and provide context for review. |
| 13 | + |
| 14 | +For the platform-agnostic sidecar schema, see [Uploading Snapshots](/product/snapshots/uploading-snapshots/#json-metadata). |
| 15 | + |
| 16 | +## Add Per-Preview Metadata |
| 17 | + |
| 18 | +Link the `SnapshotPreferences` target to the app target that contains your previews, then import it where you define previews. |
| 19 | + |
| 20 | +Use modifiers on a preview view to add metadata: |
| 21 | + |
| 22 | +```swift |
| 23 | +import SnapshotPreferences |
| 24 | + |
| 25 | +#Preview("Billing page") { |
| 26 | + BillingView() |
| 27 | + .snapshotTags([ |
| 28 | + "surface": "billing", |
| 29 | + "team": "growth", |
| 30 | + ]) |
| 31 | + .snapshotAdditionalContext([ |
| 32 | + "component": "BillingView", |
| 33 | + "variant": [ |
| 34 | + "plan": "team", |
| 35 | + "trial": true, |
| 36 | + ], |
| 37 | + ]) |
| 38 | + .snapshotDiffThreshold(0.01) |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +## SnapshotPreviews Modifiers |
| 43 | + |
| 44 | +| Modifier | Description | |
| 45 | +| ------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 46 | +| `.snapshotTags([String: String])` | Adds top-level `tags` to the sidecar for filtering or grouping snapshots. | |
| 47 | +| `.snapshotAdditionalContext([String: Any])` | Adds arbitrary key-value context to the sidecar `context` object. Values can be strings, numbers, booleans, or nested objects. | |
| 48 | +| `.snapshotDiffThreshold(Float?)` | Adds a local `diff_threshold` for this preview. Sentry reports the image as changed only when the share of changed pixels is greater than this value. | |
| 49 | + |
| 50 | +## Generated Sidecar Metadata |
| 51 | + |
| 52 | +SnapshotPreviews generates default sidecar metadata for each preview. The exact values depend on the preview and simulator, but the sidecar can look like this: |
| 53 | + |
| 54 | +```json |
| 55 | +{ |
| 56 | + "display_name": "Billing page", |
| 57 | + "group": "Checkout Preview", |
| 58 | + "tags": { |
| 59 | + "surface": "billing", |
| 60 | + "team": "growth" |
| 61 | + }, |
| 62 | + "diff_threshold": 0.01, |
| 63 | + "context": { |
| 64 | + "test_name": "-[YourAppSnapshotTest portrait-Checkout Preview-1-6]", |
| 65 | + "accessibility_enabled": false, |
| 66 | + "component": "BillingView", |
| 67 | + "variant": { |
| 68 | + "plan": "team", |
| 69 | + "trial": true |
| 70 | + }, |
| 71 | + "preview": { |
| 72 | + "container_display_name": "Checkout Preview", |
| 73 | + "container_type_name": "YourApp.CheckoutPreview", |
| 74 | + "display_name": "Billing page", |
| 75 | + "index": 1, |
| 76 | + "orientation": "portrait", |
| 77 | + "preferred_color_scheme": "dark" |
| 78 | + }, |
| 79 | + "simulator": { |
| 80 | + "device_name": "iPhone 17 Pro", |
| 81 | + "model_identifier": "iPhone18,1" |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +## Generated Context Keys |
| 88 | + |
| 89 | +SnapshotPreviews can generate these `context` keys: |
| 90 | + |
| 91 | +- `test_name` |
| 92 | +- `accessibility_enabled` |
| 93 | +- `simulator.device_name` |
| 94 | +- `simulator.model_identifier` |
| 95 | +- `preview.index` |
| 96 | +- `preview.display_name` |
| 97 | +- `preview.container_type_name` |
| 98 | +- `preview.container_display_name` |
| 99 | +- `preview.preferred_color_scheme` |
| 100 | +- `preview.orientation` |
| 101 | +- `preview.line` |
| 102 | + |
| 103 | +Additional context is merged into the generated `context` object. If a custom key matches a generated key, the custom value overrides the generated value. |
0 commit comments