|
| 1 | +# Testing Guide |
| 2 | + |
| 3 | +This project uses **Vitest** as the modern testing framework, replacing the previous Karma + Jasmine setup. Vitest provides faster test execution, better TypeScript support, and enhanced developer experience. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +```bash |
| 8 | +# Install dependencies |
| 9 | +yarn install |
| 10 | + |
| 11 | +# Run all tests once |
| 12 | +yarn test |
| 13 | + |
| 14 | +# Run tests in watch mode (development) |
| 15 | +yarn test:watch |
| 16 | + |
| 17 | +# Run tests with coverage |
| 18 | +yarn test:coverage |
| 19 | + |
| 20 | +# Open coverage report in browser |
| 21 | +yarn test:coverage:html |
| 22 | + |
| 23 | +# Run tests with UI interface |
| 24 | +yarn test:ui |
| 25 | +``` |
| 26 | + |
| 27 | +## Testing Framework |
| 28 | + |
| 29 | +### Vitest Features |
| 30 | +- ⚡ **Fast**: Native ESM support and Vite's dev server |
| 31 | +- 🔧 **Zero Config**: Works out of the box with TypeScript |
| 32 | +- 🎯 **Jest Compatible**: Same API as Jest for easy migration |
| 33 | +- 📊 **Built-in Coverage**: V8 coverage with multiple reporters |
| 34 | +- 🎨 **UI Interface**: Optional web UI for test debugging |
| 35 | +- 🔍 **Watch Mode**: Smart re-running of affected tests |
| 36 | + |
| 37 | +### Key Changes from Karma/Jasmine |
| 38 | +- `function() {}` → `() => {}` (arrow functions) |
| 39 | +- `jasmine.objectContaining()` → `expect.objectContaining()` |
| 40 | +- `toThrowError()` → `toThrow()` |
| 41 | +- Global `describe`, `it`, `expect` without imports |
| 42 | + |
| 43 | +## Test Scripts |
| 44 | + |
| 45 | +| Command | Description | |
| 46 | +|---------|-------------| |
| 47 | +| `yarn test` | Run tests once with linting | |
| 48 | +| `yarn test:watch` | Run tests in watch mode | |
| 49 | +| `yarn test:ui` | Open Vitest UI in browser | |
| 50 | +| `yarn test:coverage` | Run tests with coverage report | |
| 51 | +| `yarn test:coverage:ui` | Coverage with UI interface | |
| 52 | +| `yarn test:coverage:detailed` | Detailed coverage with thresholds | |
| 53 | +| `yarn test:coverage:html` | Generate and open HTML coverage | |
| 54 | +| `yarn test:coverage:lcov` | Generate LCOV format for CI | |
| 55 | +| `yarn test:ci` | CI-optimized run with JUnit output | |
| 56 | + |
| 57 | +## Coverage Reports |
| 58 | + |
| 59 | +### Coverage Thresholds |
| 60 | +- **Global**: 85% minimum for branches, functions, lines, statements |
| 61 | +- **Core Files**: 90% minimum for `gridstack.ts` and `gridstack-engine.ts` |
| 62 | +- **Utils**: 85% minimum for `utils.ts` |
| 63 | + |
| 64 | +### Coverage Formats |
| 65 | +- **HTML**: Interactive browser report at `coverage/index.html` |
| 66 | +- **LCOV**: For integration with CI tools and code coverage services |
| 67 | +- **JSON**: Machine-readable format for automated processing |
| 68 | +- **Text**: Terminal summary output |
| 69 | +- **JUnit**: XML format for CI/CD pipelines |
| 70 | + |
| 71 | +### Viewing Coverage |
| 72 | +```bash |
| 73 | +# Generate and view HTML coverage report |
| 74 | +yarn test:coverage:html |
| 75 | + |
| 76 | +# View coverage in terminal |
| 77 | +yarn test:coverage |
| 78 | + |
| 79 | +# Generate LCOV for external tools |
| 80 | +yarn test:coverage:lcov |
| 81 | +``` |
| 82 | + |
| 83 | +## Configuration Files |
| 84 | + |
| 85 | +- **`vitest.config.ts`**: Main Vitest configuration |
| 86 | +- **`vitest.setup.ts`**: Test environment setup and global mocks |
| 87 | +- **`.vitestrc.coverage.ts`**: Enhanced coverage configuration |
| 88 | +- **`tsconfig.json`**: TypeScript configuration with Vitest types |
| 89 | + |
| 90 | +## Writing Tests |
| 91 | + |
| 92 | +### Basic Test Structure |
| 93 | +```typescript |
| 94 | +import { Utils } from '../src/utils'; |
| 95 | + |
| 96 | +describe('Utils', () => { |
| 97 | + it('should parse boolean values correctly', () => { |
| 98 | + expect(Utils.toBool(true)).toBe(true); |
| 99 | + expect(Utils.toBool(false)).toBe(false); |
| 100 | + expect(Utils.toBool(1)).toBe(true); |
| 101 | + expect(Utils.toBool(0)).toBe(false); |
| 102 | + }); |
| 103 | +}); |
| 104 | +``` |
| 105 | + |
| 106 | +### DOM Testing |
| 107 | +```typescript |
| 108 | +describe('GridStack DOM', () => { |
| 109 | + beforeEach(() => { |
| 110 | + document.body.innerHTML = '<div class="grid-stack"></div>'; |
| 111 | + }); |
| 112 | + |
| 113 | + it('should create grid element', () => { |
| 114 | + const grid = document.querySelector('.grid-stack'); |
| 115 | + expect(grid).toBeInTheDocument(); |
| 116 | + }); |
| 117 | +}); |
| 118 | +``` |
| 119 | + |
| 120 | +### Mocking |
| 121 | +```typescript |
| 122 | +// Mock a module |
| 123 | +vi.mock('../src/utils', () => ({ |
| 124 | + Utils: { |
| 125 | + toBool: vi.fn() |
| 126 | + } |
| 127 | +})); |
| 128 | + |
| 129 | +// Mock DOM APIs |
| 130 | +Object.defineProperty(window, 'ResizeObserver', { |
| 131 | + value: vi.fn().mockImplementation(() => ({ |
| 132 | + observe: vi.fn(), |
| 133 | + unobserve: vi.fn(), |
| 134 | + disconnect: vi.fn() |
| 135 | + })) |
| 136 | +}); |
| 137 | +``` |
| 138 | + |
| 139 | +## File Organization |
| 140 | + |
| 141 | +``` |
| 142 | +spec/ |
| 143 | +├── utils-spec.ts # Utils module tests |
| 144 | +├── gridstack-spec.ts # Main GridStack tests |
| 145 | +├── gridstack-engine-spec.ts # Engine logic tests |
| 146 | +├── regression-spec.ts # Regression tests |
| 147 | +└── e2e/ # End-to-end tests (not in coverage) |
| 148 | +``` |
| 149 | + |
| 150 | +## CI/CD Integration |
| 151 | + |
| 152 | +### GitHub Actions Example |
| 153 | +```yaml |
| 154 | +- name: Run Tests |
| 155 | + run: yarn test:ci |
| 156 | + |
| 157 | +- name: Upload Coverage |
| 158 | + uses: codecov/codecov-action@v3 |
| 159 | + with: |
| 160 | + file: ./coverage/lcov.info |
| 161 | +``` |
| 162 | +
|
| 163 | +### Test Results |
| 164 | +- **JUnit XML**: `coverage/junit-report.xml` |
| 165 | +- **Coverage LCOV**: `coverage/lcov.info` |
| 166 | +- **Coverage JSON**: `coverage/coverage-final.json` |
| 167 | + |
| 168 | +## Debugging Tests |
| 169 | + |
| 170 | +### VS Code Integration |
| 171 | +1. Install "Vitest" extension |
| 172 | +2. Run tests directly from editor |
| 173 | +3. Set breakpoints and debug |
| 174 | + |
| 175 | +### Browser UI |
| 176 | +```bash |
| 177 | +yarn test:ui |
| 178 | +``` |
| 179 | +Opens a web interface for: |
| 180 | +- Running individual tests |
| 181 | +- Viewing test results |
| 182 | +- Coverage visualization |
| 183 | +- Real-time updates |
| 184 | + |
| 185 | +## Performance |
| 186 | + |
| 187 | +### Speed Comparison |
| 188 | +- **Karma + Jasmine**: ~15-20 seconds |
| 189 | +- **Vitest**: ~3-5 seconds |
| 190 | +- **Watch Mode**: Sub-second re-runs |
| 191 | + |
| 192 | +### Optimization Tips |
| 193 | +- Use `vi.mock()` for heavy dependencies |
| 194 | +- Prefer `toBe()` over `toEqual()` for primitives |
| 195 | +- Group related tests in `describe` blocks |
| 196 | +- Use `beforeEach`/`afterEach` for setup/cleanup |
| 197 | + |
| 198 | +## Migration Notes |
| 199 | + |
| 200 | +This project was migrated from Karma + Jasmine to Vitest with the following changes: |
| 201 | + |
| 202 | +1. **Dependencies**: Removed karma-\* packages, added vitest + utilities |
| 203 | +2. **Configuration**: Replaced `karma.conf.js` with `vitest.config.ts` |
| 204 | +3. **Syntax**: Updated test syntax to modern ES6+ style |
| 205 | +4. **Coverage**: Enhanced coverage with V8 provider and multiple formats |
| 206 | +5. **Scripts**: New npm scripts for various testing workflows |
| 207 | + |
| 208 | +All existing tests were preserved and converted to work with Vitest. |
0 commit comments