mixpanel-react-native
Version:
Official React Native Tracking Library for Mixpanel Analytics
97 lines (78 loc) • 3.92 kB
Markdown
# Feature Flags JavaScript Mode - Test Results & Findings
## Summary
JavaScript mode for feature flags has been successfully enabled for testing via environment variable. The implementation is mostly working but has some async operation issues that need resolution.
## What's Working ✅
1. **Environment Variable Control**: `MIXPANEL_ENABLE_JS_FLAGS=true` successfully enables JavaScript mode
2. **Basic Initialization**: Mixpanel instance creates correctly in JavaScript mode
3. **Synchronous Methods**: All sync methods work as expected:
- `areFlagsReady()`
- `getVariantSync()`
- `getVariantValueSync()`
- `isEnabledSync()`
4. **Snake-case Aliases**: API compatibility methods working
5. **Error Handling**: Gracefully handles null feature names
## Issues Found & Fixed ✅
### 1. Async Methods Timeout (FIXED)
The following async methods were hanging indefinitely (5+ second timeout):
- `loadFlags()`
- `getVariant()` (async version)
- `getVariantValue()` (async version)
- `isEnabled()` (async version)
- `updateContext()`
**Root Cause**: The MixpanelNetwork.sendRequest method was:
1. Always sending POST requests, even for the flags endpoint (which should be GET)
2. Retrying all failed requests with exponential backoff (up to 5 retries)
3. For GET requests returning 404, this caused 5+ seconds of retry delays
**Solution**: Modified `javascript/mixpanel-network.js`:
- Detect GET requests (when data is null/undefined)
- Send proper GET requests without body for flags endpoint
- Don't retry GET requests on client errors (4xx status codes)
- Only retry POST requests or server errors (5xx)
### 2. Test Suite Hanging (RESOLVED)
- **Initial Issue**: Tests would not exit after completion
- **Cause**: Recurring intervals from `mixpanel-core.js` queue processing
- **Solution**: Removed fake timers and added proper cleanup in `afterEach`
## Code Changes Made
### 1. index.js (Lines 89-95)
```javascript
// Enable JS flags for testing via environment variable
const jsFlagesEnabled = process.env.MIXPANEL_ENABLE_JS_FLAGS === 'true' ||
process.env.NODE_ENV === 'test';
// Short circuit for JavaScript mode unless explicitly enabled
if (this.mixpanelImpl !== MixpanelReactNative && !jsFlagesEnabled) {
throw new Error(
"Feature flags are only available in native mode. " +
"JavaScript mode support is coming in a future release."
);
}
```
### 2. Test File Created
- Created `__tests__/flags-js-mode.test.js` with comprehensive JavaScript mode tests
- Tests pass AsyncStorage mock as 4th parameter to Mixpanel constructor
- Proper cleanup to prevent hanging
## Next Steps
### Immediate (Before Beta Release)
1. ✅ **Fix Async Methods**: COMPLETE - Fixed network layer to handle GET requests properly
2. **Test in Real Expo App**: Run in actual Expo environment (not just unit tests)
3. **Performance Testing**: Verify AsyncStorage performance with large flag sets
### Future Enhancements
1. **Remove Blocking Check**: Once stable, remove environment variable requirement
2. **Documentation**: Update FEATURE_FLAGS_QUICKSTART.md with JS mode examples
3. **Migration Guide**: Document differences between native and JS modes
## Testing Commands
```bash
# Run JavaScript mode tests
MIXPANEL_ENABLE_JS_FLAGS=true npm test -- --testPathPattern=flags-js-mode
# Run in Expo app
cd Samples/MixpanelExpo
MIXPANEL_ENABLE_JS_FLAGS=true npm start
```
## Risk Assessment
- **Low Risk**: Core functionality works, follows established patterns
- **Low Risk**: All async operations now working correctly
- **Mitigation**: Keep behind environment variable until Expo testing complete
## Recommendations
1. ✅ Async methods fixed - ready for beta testing
2. Test in real Expo environment before removing environment variable guard
3. Consider adding a `jsMode` flag to initialization options for cleaner API
4. Monitor network performance with real API endpoints