Testing Overview
MQTT Explorer employs four types of tests:Unit Tests
Frontend component tests and backend logic tests using Mocha + Chai
UI Tests
Automated browser tests with Playwright for end-to-end validation
LLM Tests
AI assistant proposal validation and integration tests
Integration Tests
MCP protocol tests and cross-component validation
Quick Start
Run All Tests
Run Specific Test Suites
Unit Testing
Testing Framework
Stack:- Mocha - Test framework
- Chai - Assertion library
- React Testing Library - Component testing
- JSDOM - DOM implementation for Node.js
MQTT Explorer uses Mocha for all unit tests. It’s already established in the project with excellent async/await support and flexible test organization.
Frontend Tests (app/)
Location: app/src/**/*.spec.tsx
Test Utilities
Use the generic test utilities inapp/src/utils/spec/testUtils.tsx:
renderWithProviders Options
Mock Data Helpers
Testing Patterns
Example: Complete Test Suite
Seeapp/src/components/Chart/Chart.spec.tsx for a comprehensive example demonstrating:
- Multiple test groups (Rendering, Data Visualization, Edge Cases)
- Testing with different props and configurations
- Testing SVG elements
- Testing theme integration
- Performance testing
Backend Tests (backend/)
Location: backend/src/spec/*.spec.ts
Running Unit Tests
UI Automation Tests
Location:src/spec/ui-tests.spec.ts
Overview
UI tests validate core functionality through automated browser tests using Playwright. Each test is independent and deterministic.UI tests require a running MQTT broker. The helper script handles setup automatically.
Running UI Tests
Test Coverage
UI tests validate:- ✅ Connection wizard flow
- ✅ Topic tree rendering
- ✅ Message publishing
- ✅ Search functionality
- ✅ Chart visualization
- ✅ Settings persistence
- ✅ Keyboard shortcuts
Demo Video Generation
Generate documentation videos showcasing features:- mosquitto MQTT broker
- Xvfb (virtual framebuffer)
- tmux (terminal multiplexer)
- ffmpeg (video encoding)
Writing UI Tests
LLM Testing
Location:app/src/services/spec/
Test Strategy
The AI Assistant feature includes comprehensive tests to validate proposal quality and LLM integration.Offline Tests
Default mode - no API key needed. Validates structure and parsing logic.
Live Tests
Opt-in with API key. Tests real LLM responses and proposal quality.
Test Files
| File | Purpose |
|---|---|
llmService.spec.ts | Unit tests for service methods |
llmProposals.spec.ts | Proposal validation (structure, format) |
llmIntegration.spec.ts | Live LLM integration tests (opt-in) |
Running LLM Tests
Validation Criteria
Topic Validation
✅ Non-empty string✅ No wildcards (
+ or #)✅ Valid segments (no empty segments)
✅ Matches system patterns (zigbee2mqtt, homeassistant, etc.)
Payload Validation
✅ Valid JSON (if JSON format)✅ Appropriate for target system
✅ Reasonable size (< 10KB)
✅ No security risks
QoS Validation
✅ Must be 0, 1, or 2✅ Typically 0 for home automation
Description Validation
✅ Non-empty✅ Actionable (uses imperative verbs)
✅ Clear and concise
✅ Under 100 characters
Home Automation System Patterns
Environment Variables
Example Test Output
For detailed test results and examples, see docs/LLM_TEST_RESULTS.md.Integration Tests
MCP Introspection Tests
Validate Model Context Protocol integration:src/spec/testMcpIntrospection.js
Tests:
- ✅ MCP server discovery
- ✅ Tool introspection
- ✅ Resource listing
- ✅ Prompt validation
Test Best Practices
1. Test Behavior, Not Implementation
2. Use Descriptive Test Names
3. Group Related Tests
4. Keep Tests Independent
- Each test should run in isolation
- Don’t rely on test execution order
- Clean up after each test if needed
5. Test Edge Cases
- Empty data
- Null/undefined values
- Very large/small numbers
- Negative values
- Single item arrays
- Special characters
6. Use Chai Assertions
CI/CD Integration
In continuous integration pipelines:Best practices for CI/CD:
- Run offline tests by default (no API key needed)
- Optionally run live tests on schedule (e.g., nightly)
- Use secrets management for API keys
- Monitor API costs
Coverage Reporting
Troubleshooting
”ResizeObserver is not defined”
This is automatically mocked by test utilities. Ensure you’re importing fromtestUtils.tsx:
“Window is not defined”
Make surejsdom-global/register is imported in testUtils.tsx.
Tests Timing Out
Increase timeout for async operations:LLM Tests Skip Automatically
- Check that
RUN_LLM_TESTS=trueis set - Verify API key is in environment
- Check console output for skip messages
API Rate Limits
- Add delays between tests
- Use smaller test dataset
- Run tests less frequently
Test Performance
Typical execution times:- Unit tests (app): ~5-10 seconds
- Unit tests (backend): ~2-3 seconds
- UI tests: ~30-60 seconds
- LLM tests (offline): ~2 seconds
- LLM tests (live): ~30-60 seconds
Next Steps
Styling
Learn Material-UI styling conventions
Architecture
Understand the codebase structure