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.
# Install dependencies
yarn install
# Run all tests once
yarn test
# Run tests in watch mode (development)
yarn test:watch
# Run tests with coverage
yarn test:coverage
# Open coverage report in browser
yarn test:coverage:html
# Run tests with UI interface
yarn test:ui- ⚡ Fast: Native ESM support and Vite's dev server
- 🔧 Zero Config: Works out of the box with TypeScript
- 🎯 Jest Compatible: Same API as Jest for easy migration
- 📊 Built-in Coverage: V8 coverage with multiple reporters
- 🎨 UI Interface: Optional web UI for test debugging
- 🔍 Watch Mode: Smart re-running of affected tests
function() {}→() => {}(arrow functions)jasmine.objectContaining()→expect.objectContaining()toThrowError()→toThrow()- Global
describe,it,expectwithout imports
| Command | Description |
|---|---|
yarn test |
Run tests once with linting |
yarn test:watch |
Run tests in watch mode |
yarn test:ui |
Open Vitest UI in browser |
yarn test:coverage |
Run tests with coverage report |
yarn test:coverage:ui |
Coverage with UI interface |
yarn test:coverage:detailed |
Detailed coverage with thresholds |
yarn test:coverage:html |
Generate and open HTML coverage |
yarn test:coverage:lcov |
Generate LCOV format for CI |
yarn test:ci |
CI-optimized run with JUnit output |
- Global: 85% minimum for branches, functions, lines, statements
- Core Files: 90% minimum for
gridstack.tsandgridstack-engine.ts - Utils: 85% minimum for
utils.ts
- HTML: Interactive browser report at
coverage/index.html - LCOV: For integration with CI tools and code coverage services
- JSON: Machine-readable format for automated processing
- Text: Terminal summary output
- JUnit: XML format for CI/CD pipelines
# Generate and view HTML coverage report
yarn test:coverage:html
# View coverage in terminal
yarn test:coverage
# Generate LCOV for external tools
yarn test:coverage:lcovvitest.config.ts: Main Vitest configurationvitest.setup.ts: Test environment setup and global mocks.vitestrc.coverage.ts: Enhanced coverage configurationtsconfig.json: TypeScript configuration with Vitest types
import { Utils } from '../src/utils';
describe('Utils', () => {
it('should parse boolean values correctly', () => {
expect(Utils.toBool(true)).toBe(true);
expect(Utils.toBool(false)).toBe(false);
expect(Utils.toBool(1)).toBe(true);
expect(Utils.toBool(0)).toBe(false);
});
});describe('GridStack DOM', () => {
beforeEach(() => {
document.body.innerHTML = '<div class="grid-stack"></div>';
});
it('should create grid element', () => {
const grid = document.querySelector('.grid-stack');
expect(grid).toBeInTheDocument();
});
});// Mock a module
vi.mock('../src/utils', () => ({
Utils: {
toBool: vi.fn()
}
}));
// Mock DOM APIs
Object.defineProperty(window, 'ResizeObserver', {
value: vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn()
}))
});spec/
├── utils-spec.ts # Utils module tests
├── gridstack-spec.ts # Main GridStack tests
├── gridstack-engine-spec.ts # Engine logic tests
├── regression-spec.ts # Regression tests
└── e2e/ # End-to-end tests (not in coverage)
- name: Run Tests
run: yarn test:ci
- name: Upload Coverage
uses: codecov/codecov-action@v3
with:
file: ./coverage/lcov.info- JUnit XML:
coverage/junit-report.xml - Coverage LCOV:
coverage/lcov.info - Coverage JSON:
coverage/coverage-final.json
- Install "Vitest" extension
- Run tests directly from editor
- Set breakpoints and debug
yarn test:uiOpens a web interface for:
- Running individual tests
- Viewing test results
- Coverage visualization
- Real-time updates
- Karma + Jasmine: ~15-20 seconds
- Vitest: ~3-5 seconds
- Watch Mode: Sub-second re-runs
- Use
vi.mock()for heavy dependencies - Prefer
toBe()overtoEqual()for primitives - Group related tests in
describeblocks - Use
beforeEach/afterEachfor setup/cleanup
This project was migrated from Karma + Jasmine to Vitest with the following changes:
- Dependencies: Removed karma-* packages, added vitest + utilities
- Configuration: Replaced
karma.conf.jswithvitest.config.ts - Syntax: Updated test syntax to modern ES6+ style
- Coverage: Enhanced coverage with V8 provider and multiple formats
- Scripts: New npm scripts for various testing workflows
All existing tests were preserved and converted to work with Vitest.