Testing and Debugging — What Every Tutorial Skips
Lesson opener
In one line: Expo Go vs production builds for testing.
Why it matters: This shows up at work when you need to expo Go vs production builds for testing.
Before this lesson: Styling for mobile
Before you start
| Resource | Link |
|---|---|
| Previous | Styling for mobile |
| Next | Play Store publish |
| Time | ~45 minutes |
What you will learn
- Expo Go vs production builds for testing
- Where
console.logoutput appears - Exact fixes for the errors beginners hit daily
- Keyboard, network, and crash handling (critical gaps)
Section 1 — Testing tools
| Tool | When to use |
|---|---|
| Expo Go | Instant reload on real device — daily development |
| Android Emulator | No phone handy — slower, good for layout |
| Real Android device | Always test here before submit — emulator hides bugs |
| React Native Debugger | Inspect network, Redux, component tree (optional advanced) |
Android versions: Target Android 8+ (API 26+) — covers 95%+ of active devices. Test on two API levels if possible.
Section 2 — console.log on mobile
console.log('Target was', target, 'attempts', attempts);Where to see it:
- Terminal running
npx expo start— logs stream here - Expo Dev Tools in browser — open the link from terminal
- Shake device → Debug Remote JS (older workflow)
Remove or gate verbose logs before release — they slow production builds slightly and may leak data.
Section 3 — Common errors and exact fixes
"Unable to resolve module"
Cause: Stale Metro cache or wrong import path.
npx expo start --clearVerify file path and casing — ./screens/HomeScreen must match filename exactly.
"Network request failed"
Cause: fetch('http://localhost:3000') on a phone — localhost is the phone itself, not your laptop.
Fix: Use your computer's LAN IP:
// Example — replace with your ipconfig / ifconfig address
fetch('http://192.168.1.42:3000/api/scores');What breaks if ignored: Silent failure, empty lists, user sees spinner forever without error message.
App crashes on device but works in simulator
Fix: Test on real device early. Common causes: missing permission, different API level, production-only native module.
Keyboard covering TextInput
Fix: KeyboardAvoidingView — already in android5 GameScreen:
import { KeyboardAvoidingView, Platform } from 'react-native';
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : undefined}
style={{ flex: 1 }}
>
{/* form */}
</KeyboardAvoidingView>What breaks if ignored: User cannot see what they type — one-star reviews.
Section 4 — Network error handling (critical gap)
const [error, setError] = useState(null);
async function loadScores() {
setError(null);
try {
const res = await fetch(API_URL);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();
setItems(data);
} catch (e) {
setError('Could not load scores. Check your connection.');
console.error(e);
}
}
// In UI — show error to user, not only console.log
{error && <Text style={{ color: 'red' }}>{error}</Text>}Section 5 — Error boundaries (critical gap)
When a component throws, React Native shows a red error screen (dev) or blank white screen (production).
import React from 'react';
import { View, Text, Button } from 'react-native';
class ErrorBoundary extends React.Component {
state = { hasError: false };
static getDerivedStateFromError() {
return { hasError: true };
}
render() {
if (this.state.hasError) {
return (
<View style={{ flex: 1, justifyContent: 'center', padding: 24 }}>
<Text>Something went wrong.</Text>
<Button title="Try again" onPress={() => this.setState({ hasError: false })} />
</View>
);
}
return this.props.children;
}
}
// Wrap App root:
// <ErrorBoundary><NavigationContainer>...</NavigationContainer></ErrorBoundary>Plain English: Give users a recovery path instead of a white screen of death.
Section 6 — Back navigation (critical gap)
Use React Navigation stack (android3) — Android hardware back pops the stack automatically.
Do not block back without a dialog. Test: Home → Game → back → must return to Home.
Practice
Do this now: Intentionally break an import, run --clear, fix it. Add a fake fetch to wrong localhost and show user-visible error text.
Checklist
- [ ] Know where console.log appears
- [ ] Can fix module resolve with
--clear - [ ] KeyboardAvoidingView on input screens
- [ ] User-visible network errors
- [ ] Tested on real Android device
What's next
Publishing to Google Play — honest timeline and requirements.