Testing and Debugging
This document provides a comprehensive overview of the testing frameworks and debugging tools available in Mapbox GL JS. It covers how to run tests, set up debugging environments, and effectively troubleshoot issues in the library.
Overview of Testing Framework
Mapbox GL JS employs a robust testing infrastructure to ensure reliability across different environments and use cases. The testing framework consists of several test types that run on multiple platforms and browsers.
Unit Tests
Unit tests verify the functionality of individual components in isolation. These tests focus on JavaScript logic and are browser-independent.
Integration Tests
The integration test suite includes several test types:
Render Tests
Render tests compare rendered map outputs against expected images to detect visual regressions. These tests run across different browsers and platforms.
Query Tests
Query tests verify the behavior of map querying functions like queryRenderedFeatures(), ensuring that the correct features are returned when querying the map.
Other Test Types
- Expression Tests: Verify the operation of Mapbox GL JS expressions
- Style Specification Tests: Check compliance with the Mapbox GL style specification
- TypeScript Tests: Verify TypeScript type definitions
- CSP Tests: Ensure Content Security Policy compliance
- uSVG Tests: Test SVG rendering
CI/CD Pipeline
Mapbox GL JS uses CircleCI for continuous integration. The configuration defines jobs for different test types across various platforms and browsers.
Debugging Tools
Mapbox GL JS provides several debugging tools and options to help identify and fix issues.
Map Debugging Options
The devtools option can be enabled when creating a map to provide additional debugging information:
var map = new mapboxgl.Map({
container: 'map',
devtools: true,
// other options...
});Collision Box Debugging
Collision boxes for text and icons can be visualized by enabling the collisionDebug option. This helps debug symbol placement issues by showing:
- Red boxes for colliding labels (hidden)
- Blue boxes for visible labels
Debug Pages
The repository includes several HTML pages in the debug/ directory for specific debugging scenarios:
Terrain Debug
The terrain debug page allows toggling various terrain-related features:
- Terrain elevation
- Globe projection
- Satellite imagery
- Buildings
- Hillshade
- Custom layers
Other Debug Pages
- Padding Debug: Test map padding functionality
- RTL Debug: Test right-to-left text rendering
- Canvas Size Debug: Test canvas sizing issues
Running Tests Locally
To run tests locally, you can use the following npm scripts:
# Run unit tests
npm run test-unit
# Run render tests with Chrome
npm run test-render
# Run render tests with Firefox
npm run test-render-firefox
# Run query tests
npm run test-query
# Run expression tests
npm run test-expressions
# Run style specification tests
npm run test-style-specTest Directory Structure
Advanced Debugging Techniques
WebGL Debugging
Since Mapbox GL JS uses WebGL for rendering, browser WebGL debugging tools are essential:
- Chrome WebGL Inspector: Enable in Developer Tools > Settings > Experiments > WebGL Developer Tools
- Firefox WebGL Debugger: Available in Developer Tools > Settings > Available Tools > WebGL Shader Editor
Performance Profiling
Performance issues can be debugged using:
- Browser Performance Tools: Use the Performance tab in browser developer tools
- Frame Rate Counter: Enable with
map.showTileBoundaries = true; - Tile Boundaries: Enable with
map.showTileBoundaries = true;
Common Debugging Scenarios
Text Placement and Collision Issues
For debugging symbol placement and collision issues:
- Enable collision debugging to visualize collision boxes
- Check text-pitch-alignment and text-rotation-alignment settings
- Examine symbol-placement and text field configuration
Terrain and 3D Issues
For debugging terrain and 3D rendering:
- Use the terrain debug page to isolate terrain-related problems
- Check elevation data sources and terrain configuration
- Examine fill-extrusion layers for building problems
Contributing New Tests
When adding a new feature or fixing a bug, it's important to add tests:
- Unit tests for new functions and methods
- Render tests for visual changes
- Query tests for changes to querying behavior
A typical render test consists of:
- A
style.jsonfile that defines the map style and test parameters - An
expected.pngfile showing the expected rendering output
A query test requires:
- A
style.jsonfile with test configuration - An
expected.jsonfile with the expected query results
Conclusion
Mapbox GL JS provides comprehensive testing and debugging tools to ensure reliability and help troubleshoot issues. By understanding these tools and techniques, you can effectively identify and fix problems in your Mapbox GL JS applications.