This document explains how to run tests for Calx.js.
We use Jest with ts-jest for TypeScript support.
First, install the test dependencies:
npm installnpm testnpm run test:watchnpm run test:coverageCoverage report will be generated in the coverage/ directory.
Unit tests focus on individual classes and methods:
- Range.test.ts - Tests for the Range class (lowest unit of work)
- Cell.test.ts - Tests for the Cell class
- Sheet.test.ts - Tests for the Sheet class
- Workbook.test.ts - Tests for the Workbook class
- Parser.test.ts - Tests for the formula parser
Integration tests verify that components work together correctly:
- Integration.test.ts - End-to-end scenarios
- Simple calculations
- Range operations
- Dynamic recalculation
- Multi-sheet operations
- Complex formulas
- Data-driven workbooks
- Edge cases
- Performance tests
Our goal is to achieve >80% code coverage for:
- Core classes (Workbook, Sheet, Cell, Range)
- Parser and interpreter
- Dependency management
- Event system
Current coverage (run npm run test:coverage to see latest):
npm run test:coverageimport { Calx } from '../src/Calx';
describe('MyFeature', () => {
let workbook: any;
let sheet: any;
beforeEach(() => {
workbook = Calx.createWorkbook();
sheet = workbook.createSheet('TestSheet');
});
test('should do something', () => {
// Arrange
sheet.createCell('A1', { value: 100 });
// Act
const range = sheet.getRange('A1');
// Assert
expect(range.value).toBe(100);
});
});describe('Integration: Feature X', () => {
test('should work end-to-end', () => {
// Create workbook
const workbook = Calx.createWorkbook();
const sheet = workbook.createSheet('Sheet1');
// Set up data
sheet.createCell('A1', { value: 10 });
sheet.createCell('A2', { formula: '=A1*2' });
// Build and calculate
workbook.build();
workbook.calculate();
// Verify results
expect(sheet.getCellValue('A2')).toBe(20);
});
});Focus on individual components in isolation:
- ✅ Range operations
- ✅ Cell creation and values
- ✅ Sheet management
- ✅ Workbook creation
- ✅ Parser expressions
- ✅ Formula functions
Test interactions between components:
- ✅ End-to-end calculations
- ✅ Range-based workflows
- ✅ Dynamic recalculation
- ✅ Multi-sheet workbooks
- ✅ Complex formula chains
- ✅ Data-driven initialization
Verify performance with large datasets:
- ✅ Large number of cells (1000+)
- ✅ Deep dependency chains (100+ levels)
- ✅ Complex calculations
⚠️ Memory usage (TODO)⚠️ Calculation speed benchmarks (TODO)
Handle unusual scenarios:
- ✅ Empty formulas
- ✅ Circular references
- ✅ Division by zero
- ✅ Non-existent cells
- ✅ Reverse ranges (B2:A1)
Tests should run automatically on:
- Every commit (pre-commit hook)
- Every pull request
- Every merge to main branch
# Add to .git/hooks/pre-commit
#!/bin/sh
npm testnpx jest test/Range.test.tsnpx jest -t "should create range for single cell"Add to .vscode/launch.json:
{
"type": "node",
"request": "launch",
"name": "Jest Debug",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["--runInBand", "--no-cache"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}| Component | Target Coverage | Current |
|---|---|---|
| Range | 90% | TBD |
| Cell | 85% | TBD |
| Sheet | 85% | TBD |
| Workbook | 80% | TBD |
| Parser | 85% | TBD |
| Overall | 80% | TBD |
- ✅ All tests passing
- ✅ No console errors
- ✅ No memory leaks
- ✅ Fast execution (<10s for all tests)
- 🔄 100% deterministic (no flaky tests)
test('range operation', () => {
const range = sheet.getRange('A1:B2');
expect(range.count).toBe(4);
expect(range.isSingleCell()).toBe(false);
});test('formula calculation', () => {
sheet.createCell('A1', { value: 10 });
sheet.createCell('A2', { formula: '=A1*2' });
workbook.build();
workbook.calculate();
expect(sheet.getCellValue('A2')).toBe(20);
});test('cell change event', () => {
const events: any[] = [];
sheet.dispatcher.listen(CellEvent.VALUE_CHANGED, (event) => {
events.push(event);
});
sheet.createCell('A1', { value: 100 });
sheet.getCellDirect('A1').value = 200;
expect(events.length).toBeGreaterThan(0);
});- Check Jest is installed:
npm list jest - Check TypeScript config:
tsconfig.test.json - Run with verbose:
npm test -- --verbose
- Ensure
@types/jestis installed - Check
tsconfig.test.jsonincludes test files - Restart TypeScript server in VS Code
- Check module resolution in
jest.config.js - Verify file paths are correct
- Check
moduleNameMapperconfiguration
- ✅ Run all tests:
npm test - ✅ Check coverage:
npm run test:coverage - 📝 Write tests for new features
- 🎯 Improve coverage to >80%
- 🚀 Set up CI/CD pipeline