bimplus-renderer
Version:
bim+ renderer
64 lines (50 loc) • 2.65 kB
Markdown
# Test-Only Moment-Timezone Integration
## Overview
This solution successfully implements moment-timezone dependency injection specifically for tests while keeping the production code completely free of the dependency.
## Implementation
### 1. Production Code (Optional Dependency)
- **[src/viewport3d.js](src/viewport3d.js)**: Contains optional moment-timezone detection
- **[src/utils/momentTimezoneInjector.js](src/utils/momentTimezoneInjector.js)**: Utility for external injection (for BimExplorer app)
- Production builds work without moment-timezone installed
- Runtime errors provide clear instructions when moment-timezone features are needed
### 2. Test-Only Injection
- **[test/setup/momentTimezoneInjector.js](test/setup/momentTimezoneInjector.js)**: Test setup that injects moment-timezone globally
- **package.json**: moment-timezone installed as `devDependency` only
- **karma-files-config.js**: Loads injector before any test files
### 3. Test Configuration
```javascript
// In karma-files-config.js
files: [
"src/**/*.js",
// Load test environment setup (inject moment-timezone for tests)
"test/setup/momentTimezoneInjector.js",
// Load custom assertions first
"test/customAsserts.js",
// Load test files
"test/**/*.js",
],
```
## How It Works
### For Tests
1. Karma loads the moment-timezone injector first
2. The injector imports moment-timezone (available as dev dependency)
3. Injects it into global scope (`window.moment`)
4. All tests run with moment-timezone available
5. Sun calculation tests pass with accurate timezone handling
### For Production
1. Production code uses optional detection: `getMomentTZ()`
2. If moment-timezone isn't found, production code either:
- Throws descriptive error for features requiring it (`setSunPosition`)
3. Build process excludes moment-timezone from production bundle
4. Users can inject moment-timezone via BimExplorer app if needed
## File Changes
### New Files
- `test/setup/momentTimezoneInjector.js` - Test environment setup
### Modified Files
- `karma-files-config.js` - Added test setup loader
- `package.json` - Added moment-timezone as devDependency
### Existing Files (Unchanged)
- `src/viewport3d.js` - Already has optional moment-timezone detection
- `src/utils/momentTimezoneInjector.js` - Already provides injection utilities
- All test files - Work seamlessly with injected moment-timezone
This solution provides the best of both worlds: comprehensive test coverage with accurate timezone calculations, while maintaining a lean production build without unnecessary dependencies.