---
updatedAt: 2025-07-02T21:11:42.000Z
agentTools:
  projectIndex: https://docs.emergetools.com/llms.txt
---

# Reducing flakes

Previews need to be deterministic in order to render consistently between snapshot runs.

Here are a few tips for common issues we see:

1. Don't perform any live network calls. Instead, use a fake network client that serves requests from disk.
2. Avoid displaying random values like `UUID`.
3. Display fixed dates rather than the current time.
4. Use a fixed build version.

# Android

## Lottie flakiness

Emerge recommends forcing Lottie to run on synchronously as a part of any Preview that relies on Lottie animations:

```kotlin MyPreview.kt
@Preview
fun MyPreview() {
  LottieTask.EXECUTOR = Executor(Runnable::run)
  MyComposable { .. }
}
```

## Custom Precision

You can reduce the amount of precision required for images to be considered unchanged using  the `@EmergeSnapshotConfig` annotation's `precision` parameter.

```kotlin
@EmergeSnapshotConfig(precision = 0.95f)
@Preview
@Composable
fun MyPreview() { ... }
```

In the above example, any diff < 5% will not be flagged for the `MyPreview` preview.

You'll need to add a dependency on the `snapshots-runtime` artifact to use the\
`@EmergeSnapshotConfig` annotation. This is a lightweight library only containing annotations and has a negligible impact on app size.

```kotlin
dependencies {
  debugImplementation("com.emergetools.snapshots:snapshots-runtime:{latest_version}")
}
```

Latest annotations version: [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.emergetools.snapshots/snapshots-annotations/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.emergetools.snapshots/snapshots-annotations)

## Changing logic if preview is running in snapshot environment

Sometimes when running snapshots, it's advantageous to know if the Preview is running in Emerge's\
snapshot testing environment to change logic, such as setting default dates/times or other
snapshot-specific logic.

As of the 1.5.0 release of snapshots, the `snapshots-runtime` artifact exposes a compose state\
provider, `LocalEmergeSnapshotMode` which can be used to determine if a preview is running in
Emerge's snapshotting environment.

```kotlin
@Preview
@Composable
fun MyComposablePreview() {
  val isSnapshotTest = LocalEmergeSnapshotMode.current

  val text = if (LocalEmergeSnapshotMode.current) {
    "Emerge Snapshot title"
  } else {
    "Title"
  }
  MyComposable(
    text = text
  )
}
```

An implementation dependency on the snapshots-runtime library is required for using the\
`LocalEmergeSnapshotMode`:

```kotlin
dependencies {
  debugImplementation("com.emergetools.snapshots:snapshots-runtime:{latest_version}")
}
```

Latest runtime\
version: [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.emergetools.snapshots/snapshots-runtime/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.emergetools.snapshots/snapshots-runtime)

# iOS

To help control for flakiness, HTTP requests through NSURL are blocked while generating snapshots.

If you'd like to conditionally set code when a preview is being rendered by our system, you can check whether the `EMERGE_IS_RUNNING_FOR_SNAPSHOTS` environment variable is set to `"1"`. For example:

```swift swift
if ProcessInfo.processInfo.environment["EMERGE_IS_RUNNING_FOR_SNAPSHOTS"] == "1" {
  return "10 minutes ago"
}
```

## Custom Precision

You can reduce the amount of precision required for images to be considered unchanged using `EmergePrecisionModifier`. Add it to your preview by linking `SnapshotPreviewsCore` from the [SDK](https://github.com/EmergeTools/SnapshotPreviews-iOS) and calling `.emergeSnapshotPrecision(...)` For example the following preview will only be marked as changed if the diff is greater than 1%.

```Text Swift
struct MyComponent_Previews: PreviewProvider {
    static var previews: some View {
        MyView()
            .previewDisplayName("Light")
            .emergeSnapshotPrecision(0.99)
    }
}
```

The percentage difference is calculated based on the perceptual difference between pixels, with colors that are more different having a higher percent difference.