mirror of
https://github.com/rustfs/console.git
synced 2026-08-28 19:47:21 +08:00
7a08d132f0
- Remove all references to performance tests - Update npm commands to pnpm commands - Update GitHub Actions example to use pnpm - Ensure all content is in English - Update test structure documentation
7.0 KiB
7.0 KiB
Config Helpers Test Suite
This is a comprehensive test suite for the config-helpers.ts module, including unit tests, integration tests, and edge case tests.
Test Structure
tests/
├── setup.ts # Test environment setup
├── utils/
│ ├── config-helpers.test.ts # Main unit tests
│ ├── config-helpers.integration.test.ts # Integration tests and edge cases
│ └── test-helpers.ts # Test utilities and Mock functions
└── README.md # This file
Installing Test Dependencies
# Install test dependencies
pnpm add -D vitest jsdom @vitest/ui c8
Running Tests
Basic Test Commands
# Run all tests
pnpm test
# Run tests and generate coverage report
pnpm test:coverage
# Run test UI interface
pnpm test:ui
# Run tests once (CI mode)
pnpm test:run
Targeted Tests
# Run only config-helpers related tests
pnpm test:config-helpers
# Run only integration tests
pnpm test:integration
# Run specific test file
pnpm exec vitest tests/utils/config-helpers.test.ts
# Run specific test case
pnpm exec vitest -t "getCurrentBrowserConfig"
Test Coverage
1. Unit Tests (config-helpers.test.ts)
- ✅ createDefaultConfig: Default configuration creation
- ✅ getCurrentBrowserConfig: Browser configuration retrieval
- ✅ getStoredHostConfig: localStorage configuration retrieval
- ✅ fetchConfigFromServer: Server configuration retrieval and merging
- ✅ getConfig: Smart configuration strategy
- ✅ saveHostConfig: Configuration saving
- ✅ clearStoredHostConfig: Configuration clearing
- ✅ validateConfig: Configuration validation
- ✅ getConfigSources: Debug information retrieval
- ✅ Legacy Functions: Backward compatibility functions
2. Integration Tests (config-helpers.integration.test.ts)
- ✅ Complete Configuration Flow: Save → Retrieve → Validate → Clear
- ✅ Configuration Priority: server > localStorage > browser > default
- ✅ Edge Cases: Extreme URLs, special characters, error handling
- ✅ Concurrent Operations: Race conditions, concurrent read/write
- ✅ Cross-browser Compatibility: Behavior in different environments
- ✅ Error Recovery: Network errors, JSON parsing errors, storage exceptions
Test Utilities (test-helpers.ts)
Mock Utilities
- BrowserMock: Mock browser environment and window.location
- LocalStorageMock: Mock localStorage behavior and exceptions
- FetchMock: Mock various network response scenarios
Data Generators
- createTestConfig: Generate test configuration objects
- createTestServerResponse: Generate server response data
- generateUrlTestCases: Generate URL test cases
- generateConfigTestCases: Generate configuration test cases
Performance Utilities
- measureExecutionTime: Measure function execution time
- runBatchTest: Execute tests in batch
- TestCleaner: Test environment cleanup
Assertion Helpers
- expectValidConfig: Validate configuration object structure
- expectValidConfigResult: Validate ConfigResult structure
- expectErrorResult: Validate error results
Test Scenarios
Normal Flow Tests
- Configuration Creation: Verify correctness of default configuration generation
- Configuration Retrieval: Test retrieval logic for various configuration sources
- Configuration Merging: Verify merging of server configuration with default configuration
- Configuration Saving: Test configuration persistence functionality
- Configuration Validation: Verify configuration completeness checks
Exception Handling Tests
- Network Exceptions: Timeouts, connection failures, invalid responses
- Storage Exceptions: localStorage unavailable, quota exceeded
- Data Exceptions: Invalid URLs, corrupted JSON, missing fields
- Environment Exceptions: Non-browser environment, missing APIs
Edge Case Tests
- Extreme Data: Ultra-long URLs, large configuration objects, special characters
- Concurrent Operations: Simultaneous read/write, race conditions, resource contention
- Performance Limits: Large amounts of data, high-frequency operations, memory pressure
CI/CD Integration
GitHub Actions Example
name: Test Config Helpers
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 10.19.0
- uses: actions/setup-node@v4
with:
node-version: '22'
cache: 'pnpm'
- run: pnpm install --frozen-lockfile
- run: pnpm test:run
- run: pnpm test:coverage
Coverage Requirements
- Statement Coverage: > 95%
- Branch Coverage: > 90%
- Function Coverage: 100%
- Line Coverage: > 95%
Debugging Tips
Running Single Tests
# Using describe or it names
pnpm exec vitest -t "getCurrentBrowserConfig should create config based on current browser location"
# Using file path and line number
pnpm exec vitest tests/utils/config-helpers.test.ts:45
Viewing Detailed Output
# Show detailed test output
pnpm exec vitest --reporter=verbose
# Show coverage details
pnpm exec vitest --coverage --reporter=verbose
Debug Mode
# Run in Node.js debug mode
pnpm exec vitest --inspect-brk
# Using VS Code debugger
# Add configuration in .vscode/launch.json
Best Practices
- Test Isolation: Each test should be independent and not depend on other tests' state
- Mock Management: Reset mocks in
beforeEach, clean up inafterEach - Clear Assertions: Use specific assertions, avoid overly broad checks
- Error Testing: Test both success and failure paths
- Code Coverage: Maintain high coverage (> 95%), focus on testing edge conditions and error paths
Troubleshooting
Common Issues
- localStorage Unavailable: Ensure localStorage is properly mocked in test setup
- fetch Undefined: Ensure fetch is mocked in setup.ts
- window Object Missing: Use jsdom environment or properly mock window
- Async Test Timeout: Increase timeout or optimize async logic
Debugging Steps
- Check test setup file (
setup.ts) - Verify mock configuration is correct
- Review test output and error messages
- Use
console.logor debugger to check state - Run single test to isolate issues
Contributing Guidelines
Adding New Tests
- Determine test type (unit/integration)
- Choose appropriate test file
- Use existing test utilities and mocks
- Follow naming conventions and structure
- Add necessary documentation comments
Test Naming Conventions
- Use descriptive English test descriptions
- Format:
should + expected behavior - Example:
should fallback to browser config when server config fails
Code Coverage
- New features must have corresponding tests
- Maintain high coverage (> 95%)
- Focus on testing edge conditions and error paths