swiftui · note · architecture · environment

Preview Dependency Injection Checklist

Published July 16, 2026 · 2 min read · beginner

Context

Previews break most often when a feature depends on environment values, services, or theme tokens that are easy to forget in the preview provider. This checklist keeps the injection path obvious so previews look like the real screen instead of a half-wired mock.

The snippet

#Preview {
    ProScreen()
        .environment(\.isProFeatureEnabled, true)
}

Why this works

The preview now states its dependency directly. That makes the feature easier to inspect because the setup is close to the view that needs it. It also reduces accidental coupling to app-level defaults that may not reflect the state you want to test.

Use this checklist:

  • inject every environment dependency the screen reads
  • provide a safe default for services that should not run in previews
  • keep preview setup close to the feature
  • test both the default state and the special-case state
  • make sure preview failures reveal missing dependencies immediately

If a preview still works with no injected values when it should not, the environment default is probably hiding a problem.

Use it when…

  • a feature reads @Environment values
  • a preview needs to simulate a paid, offline, or feature-flagged state
  • a screen depends on injected services or feature-specific configuration

Avoid it when…

  • the screen does not read any injected dependencies
  • the preview setup becomes more complex than the screen itself
  • you are stuffing view-specific data into environment just to avoid parameters

Takeaway: Good previews make dependencies visible, not invisible.

Next steps