Testing Guide
Wiremd includes comprehensive test coverage for all features, including the live preview functionality.
Test Overview
Total Tests: 152
Test Files
parser.test.ts (29 tests)
- Markdown parsing
- AST transformation
- Custom syntax handling
renderer.test.ts (19 tests)
- HTML rendering
- Style application
- Component rendering
server.test.ts (31 tests)
- Dev server functionality
- WebSocket communication
- Live-reload injection
- Error overlay
- Viewport switcher
- Connection status
cli.test.ts (38 tests)
- CLI command parsing
- File generation
- Style and format options
- Watch mode
- Error handling
- Server integration
integration.test.ts (35 tests)
- End-to-end live preview flow
- CLI and server integration
- WebSocket message handling
- UI component integration
- Feature coverage verification
Running Tests
Run all tests
npm testRun tests in watch mode
npm run test:watchRun tests with coverage
npm run test:coverageRun specific test file
npm test -- tests/server.test.tsTest Coverage
Dev Server Tests (server.test.ts)
Tests the enhanced dev server with live preview features:
- ✅ Server startup and configuration
- ✅ HTML file serving with script injection
- ✅ Live-reload script injection
- ✅ WebSocket connection handling
- ✅ Error notification system
- ✅ Reload notification system
- ✅ Viewport switcher UI
- ✅ Error overlay styling and behavior
- ✅ Connection status indicator
- ✅ Reconnection logic
- ✅ Toolbar UI components
- ✅ Preview wrapper functionality
CLI Tests (cli.test.ts)
Tests the command-line interface:
- ✅ Help command display
- ✅ Version command
- ✅ File generation (HTML/JSON)
- ✅ All style options (sketch, clean, wireframe, material, tailwind, brutal, none)
- ✅ Format options validation
- ✅ Error handling and messages
- ✅ Output path handling
- ✅ Server integration
- ✅ Watch mode functionality
- ✅ Error notification integration
- ✅ Live reload integration
- ✅ Signal handling (SIGINT)
- ✅ Console output formatting
Integration Tests (integration.test.ts)
Tests the complete live preview flow:
- ✅ CLI and server function imports
- ✅ Complete reload flow (file change → regenerate → notify)
- ✅ Complete error flow (file change → error → notify error)
- ✅ WebSocket message formatting
- ✅ Client-side message handling
- ✅ UI component injection order
- ✅ Content wrapping logic
- ✅ Viewport switcher integration
- ✅ Connection status updates
- ✅ Error overlay behavior
- ✅ TypeScript configuration
- ✅ Documentation completeness
- ✅ Package dependencies
- ✅ Feature coverage verification
Test Structure
Unit Tests
Each component is tested in isolation:
describe('Component', () => {
it('should do something', () => {
// Test implementation
});
});Integration Tests
Tests verify that components work together:
describe('Feature Integration', () => {
it('should complete full workflow', () => {
// Test end-to-end flow
});
});Writing Tests
Test Naming
Use descriptive test names:
// ✅ Good
it('should inject viewport switcher buttons', () => {});
// ❌ Bad
it('test viewport', () => {});Test Structure
Follow Arrange-Act-Assert pattern:
it('should handle error messages', () => {
// Arrange
const errorMessage = 'Parse failed';
// Act
notifyError(errorMessage);
// Assert
expect(/* result */).toBe(/* expected */);
});Async Tests
For async operations, use async/await:
it('should start server', async () => {
const result = await startServer({ port: 3000 });
expect(result).toBeDefined();
});Continuous Integration
Tests run automatically on:
- Every commit
- Pull requests
- Before publishing
CI Configuration
# Example GitHub Actions workflow
- name: Run tests
run: npm test
- name: Upload coverage
run: npm run test:coverageCoverage Goals
Current coverage targets:
- Overall: 80%+
- Critical paths: 100%
- UI components: 90%+
- Error handling: 100%
View coverage report:
npm run test:coverageCommon Test Patterns
Testing File Operations
beforeEach(() => {
writeFileSync('test.md', '# Test');
});
afterEach(() => {
unlinkSync('test.md');
});Testing CLI Commands
it('should generate output', () => {
const result = execSync('node dist/cli/index.js input.md', {
encoding: 'utf-8'
});
expect(result).toContain('Generated');
});Testing Source Code
For features that inject code, test the source:
it('should include feature code', () => {
const source = readFileSync('./src/file.ts', 'utf-8');
expect(source).toContain('featureCode');
});Debugging Tests
Run single test
npm test -- -t "test name"Run with verbose output
npm test -- --reporter=verboseDebug in VS Code
Add to .vscode/launch.json:
{
"type": "node",
"request": "launch",
"name": "Debug Tests",
"program": "${workspaceFolder}/node_modules/vitest/vitest.mjs",
"args": ["run"],
"console": "integratedTerminal"
}Test Utilities
Mock Server
For testing server functionality without actual network:
import { vi } from 'vitest';
vi.mock('http', () => ({
createServer: vi.fn()
}));Temporary Files
Always clean up test files:
afterEach(() => {
try {
unlinkSync(testFile);
} catch (e) {
// File may not exist
}
});Performance Tests
Monitor test performance:
npm test -- --reporter=verboseSlow tests (>1s) should be optimized or moved to integration suite.
Future Test Plans
- [ ] E2E browser tests for live preview UI
- [ ] Performance benchmarks
- [ ] Stress tests for WebSocket connections
- [ ] Visual regression tests for rendered output
- [ ] Accessibility tests
- [ ] Cross-browser testing
Contributing
When adding features:
- Write tests first (TDD)
- Ensure all tests pass
- Add integration tests for new flows
- Update this document
- Maintain >80% coverage