Snapshot testing captures the rendered output of a component and saves it as a file. Later, it compares the current output to the saved snapshot to detect unexpected changes.
Click to reveal answer
beginner
How do you create a snapshot test for a React Native component?
You render the component using a test renderer, then use expect(tree).toMatchSnapshot() to save or compare the snapshot.
Click to reveal answer
beginner
Why is snapshot testing useful in mobile app development?
It helps catch UI changes early by comparing the current UI output to a saved version, preventing accidental visual bugs.
Click to reveal answer
intermediate
What should you do if a snapshot test fails because of an intentional UI change?
Review the changes and if they are expected, update the snapshot file by running the test command with the update flag (e.g., jest -u).
Click to reveal answer
intermediate
Name one limitation of snapshot testing.
Snapshot tests can pass even if the UI looks wrong, because they only compare output text, not actual rendered visuals or user experience.
Click to reveal answer
What does a snapshot test compare in React Native?
AThe rendered component output to a saved snapshot file
BThe app's performance metrics
CThe network requests made by the app
DThe app's database contents
✗ Incorrect
Snapshot tests compare the current rendered output of a component to a previously saved snapshot file to detect changes.
Which command updates snapshot files when UI changes are intentional?
Ajest --watch
Bjest -u
Cnpm start
Dreact-native run-ios
✗ Incorrect
The jest -u command updates snapshot files to reflect intentional UI changes.
Snapshot testing is best described as:
AValidating backend API responses
BTesting app performance under load
CTesting user interactions manually
DChecking if UI output matches a saved version
✗ Incorrect
Snapshot testing checks if the UI output matches a saved snapshot to catch unexpected changes.
What tool is commonly used for snapshot testing in React Native?
AJest
BRedux
CExpo
DFirebase
✗ Incorrect
Jest is the testing framework commonly used for snapshot testing in React Native.
If a snapshot test fails unexpectedly, what should you do first?
AIgnore the failure
BDelete the snapshot file
CReview the UI changes to confirm if they are intentional
DRestart the device
✗ Incorrect
You should review the UI changes to confirm if they are intentional before updating snapshots.
Explain how snapshot testing helps maintain UI consistency in React Native apps.
Think about how saving and comparing UI output can catch bugs.
You got /4 concepts.
Describe the steps to write and update a snapshot test for a React Native component.
Focus on the test code and commands used.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of snapshot testing in React Native?
easy
A. To check the app's performance speed
B. To test network requests and API responses
C. To detect unexpected changes in the UI output
D. To validate user input formats
Solution
Step 1: Understand snapshot testing goal
Snapshot testing saves the UI output to detect changes later.
Step 2: Identify what snapshot testing checks
It compares future UI renders to saved snapshots to catch unexpected changes.
Final Answer:
To detect unexpected changes in the UI output -> Option C
Quick Check:
Snapshot testing = UI change detection [OK]
Hint: Snapshot testing checks UI changes, not logic or speed [OK]
Common Mistakes:
Confusing snapshot testing with API or performance tests
Thinking it tests user input validation
Assuming it measures app speed
2. Which of the following is the correct way to write a basic snapshot test in React Native using Jest?
easy
A. render().toMatchSnapshot();
B. expect(render()).toMatchSnapshot();
C. expect().toMatchSnapshot();
D. expect(render()).toBeSnapshot();
Solution
Step 1: Recall Jest snapshot syntax
Jest requires wrapping the rendered output in expect() and calling toMatchSnapshot().
Step 2: Check each option's syntax
expect(render()).toMatchSnapshot(); correctly wraps render output in expect() and calls toMatchSnapshot(). Others miss expect() or use wrong matcher.
Final Answer:
expect(render()).toMatchSnapshot(); -> Option B
Quick Check:
Use expect(render()).toMatchSnapshot() [OK]
Hint: Always wrap render output in expect() before toMatchSnapshot() [OK]
Common Mistakes:
Omitting expect() around render output
Using incorrect matcher like toBeSnapshot()
Passing component directly to expect() without render
3. Given this snapshot test code:
const tree = render().toJSON();
expect(tree).toMatchSnapshot();
What will happen if the Button's title changes to "Press" in the next test run?
medium
A. The test will fail because the snapshot does not match
B. The test will pass because title changes are ignored
C. The test will throw a syntax error
D. The test will pass but log a warning
Solution
Step 1: Understand snapshot comparison
Snapshots save the UI output including props like title. Changing title changes output.
Step 2: Effect of changing title on test
Since the new render differs from saved snapshot, Jest will fail the test to alert the change.
Final Answer:
The test will fail because the snapshot does not match -> Option A
Quick Check:
Changed UI props cause snapshot test failure [OK]
Hint: Any UI change breaks snapshot test unless updated [OK]