Course contentsShow
← React
Lesson 2158 of 2,39647. Testing Patterns and Best PracticesFree lesson

Inline Snapshots vs External Files

Inline snapshots in the test file improve reviewability but clutter code; external snapshots hide changes in diffs.

Inline Snapshots vs External Files

What you'll learn: How choosing between inline and external snapshots affects code review and maintainability in your test suite.

The Two Snapshot Flavors

When you create a snapshot test, you have two options for where the snapshot data lives:

External snapshots (the default) get written to separate __snapshots__/*.snap files. The test file just contains the assertion, while the expected output lives elsewhere.

Inline snapshots embed the expected output directly in your test file as a string literal using toMatchInlineSnapshot().

The Tradeoff

External snapshots keep your test files clean and focused on logic. However, they create a hidden problem during code reviews: the diff only shows changes to the .snap file. Reviewers must mentally connect "this component changed" with "these snapshot bytes changed" across two separate files. It's like reading a conversation where questions and answers are in different documents.

Inline snapshots solve the review problem beautifully. When someone changes a component, the PR diff shows both the code change AND the snapshot change right there in the test file. Reviewers can immediately see "Oh, we added a button, and yes, here's that button in the snapshot."

The cost? Code clutter. Your test file now contains potentially hundreds of lines of HTML strings. Finding the actual test logic becomes harder. It's like reading a recipe where the ingredient list is mixed throughout the instructions instead of listed at the top.

Practical Guidance

Use inline snapshots for:

  • Small, focused component outputs (a few lines)
  • When team code review is critical
  • Critical UI that changes frequently

Use external snapshots for:

  • Large component trees
  • When test readability matters more than diff visibility

Key Takeaway: Inline snapshots improve code review transparency at the cost of test file readability; choose based on whether reviewers need immediate visual confirmation of UI changes.