Skip to content

Commit 61d5ac2

Browse files
authored
Test (#50)
* feat: add vitest * feat: add tests * fix: test config * feat: add github ci * chore: update md's
1 parent 06e6dc8 commit 61d5ac2

File tree

19 files changed

+3244
-104
lines changed

19 files changed

+3244
-104
lines changed

.github/workflows/ci-dev.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ jobs:
3232

3333
- name: Build
3434
run: npm run build
35+
36+
- name: Run Tests
37+
run: npm run test

.github/workflows/ci-full.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ jobs:
3333
- name: Build
3434
run: npm run build
3535

36+
- name: Run Tests
37+
run: npm run test
38+
3639
tag:
3740
name: Version tag
3841
needs: [check]

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ dist
77
# TypeScript cache
88
*.tsbuildinfo
99

10+
# Test coverage
11+
coverage/
12+
1013
# Optional npm cache directory
1114
.npm
1215

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Added
11+
12+
- Unit testing infrastructure using Vitest
13+
- Comprehensive test coverage (~68%) for core modules:
14+
- `commandLine.ts` (84.56%): CLI argument parsing and validation
15+
- `file.ts` (100%): File operations and duplicate detection
16+
- `cppCode.ts` (96.62%): C++ code generation and templates
17+
- `consoleColor.ts` (100%): Console utilities
18+
- Test fixtures for validating file processing
19+
- Coverage reports with HTML output
20+
- Development documentation for testing in README.md and CLAUDE.md
21+
22+
### Changed
23+
24+
- Updated `.gitignore` to exclude `coverage/` directory
25+
- Enhanced documentation with testing sections
26+
827
## [1.10.0] - 2025-11-20
928

1029
### Changed

CLAUDE.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,16 @@ npm run dev:psychic
2626
# Development with live reload (psychic2 engine)
2727
npm run dev:psychic2
2828

29-
# Run comprehensive tests (requires PlatformIO)
29+
# Run TypeScript unit tests
30+
npm run test
31+
32+
# Run tests in watch mode
33+
npm run test:watch
34+
35+
# Generate test coverage report
36+
npm run test:coverage
37+
38+
# Run comprehensive ESP32 tests (requires PlatformIO)
3039
npm run test:all
3140
```
3241

@@ -119,6 +128,53 @@ npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./output.h --etag=true
119128
- **Prettier**: 120 character line width, single quotes, no trailing commas
120129
- **Import Sorting**: Automatic import organization with `simple-import-sort`
121130

131+
### Testing
132+
133+
The project uses **Vitest** for unit testing with comprehensive coverage:
134+
135+
**Test Structure:**
136+
137+
```
138+
test/
139+
├── unit/
140+
│ ├── commandLine.test.ts # CLI argument parsing tests
141+
│ ├── file.test.ts # File operations tests
142+
│ ├── cppCode.test.ts # C++ code generation tests
143+
│ └── consoleColor.test.ts # Console utilities tests
144+
└── fixtures/
145+
└── sample-files/ # Test fixture files
146+
```
147+
148+
**Test Coverage (68.25% overall):**
149+
150+
- `commandLine.ts`: 84.56% - CLI argument parsing, validation, engine/tri-state validation
151+
- `file.ts`: 100% - File collection, duplicate detection (SHA256), pre-compressed file skipping
152+
- `cppCode.ts`: 96.62% - Template rendering for all 4 engines, etag/gzip combinations, Handlebars helpers
153+
- `cppCodeEspIdf.ts`: 100% - ESP-IDF template (tested via `cppCode.ts`)
154+
- `consoleColor.ts`: 100% - ANSI color code wrapping
155+
- `index.ts`: 0% - Main entry point (has side effects, tested via integration)
156+
157+
**Key Testing Features:**
158+
159+
- **Vitest Configuration**: `vitest.config.ts` with TypeScript support, 60% coverage thresholds
160+
- **Mocking Strategy**: Uses `vi.mock()` for file system (`node:fs`), glob (`tinyglobby`), and module dependencies
161+
- **Dynamic Imports**: Tests use dynamic imports for `commandLine.ts` to test different CLI arguments without side effects
162+
- **Test Fixtures**: Small sample files (HTML/CSS/JS) for testing file processing pipeline
163+
- **Coverage Reports**: Generated in `coverage/` directory (ignored by git), viewable HTML reports at `coverage/index.html`
164+
165+
**Testing Approach:**
166+
167+
- **commandLine.test.ts**: Tests argument parsing (`--flag=value`, `-f value`, `--flag value`), validation errors, required arguments, directory validation. Uses dynamic imports to avoid module side effects.
168+
- **file.test.ts**: Mocks file system with `memfs`, tests duplicate detection, compressed file skipping, path handling
169+
- **cppCode.test.ts**: Tests template selection by engine, code generation for all etag/gzip combinations, byte array conversion, ETag/cache headers, default route detection
170+
- **consoleColor.test.ts**: Simple tests for ANSI escape code wrapping (quick coverage wins)
171+
172+
**Running Tests:**
173+
174+
- `npm run test` - Run all tests once (CI/CD)
175+
- `npm run test:watch` - Watch mode for development
176+
- `npm run test:coverage` - Generate coverage reports
177+
122178
### Demo Projects
123179

124180
- **`demo/svelte/`**: Example Svelte application with Vite, TailwindCSS, gallery images

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,51 @@ This npm package provides a solution for **inserting any JS client application i
3737
- Node.js >= 20
3838
- npm >= 9
3939

40+
### Development
41+
42+
#### Testing
43+
44+
The project includes comprehensive unit tests using Vitest:
45+
46+
```bash
47+
# Run tests once
48+
npm run test
49+
50+
# Run tests in watch mode (for development)
51+
npm run test:watch
52+
53+
# Generate coverage report
54+
npm run test:coverage
55+
```
56+
57+
**Test Coverage:** ~68% overall with focus on core functionality:
58+
59+
- `commandLine.ts`: 84.56% - CLI argument parsing and validation
60+
- `file.ts`: 100% - File operations and duplicate detection
61+
- `cppCode.ts`: 96.62% - C++ code generation and templates
62+
- `consoleColor.ts`: 100% - Console output utilities
63+
64+
Coverage reports are generated in the `coverage/` directory and can be viewed by opening `coverage/index.html` in a browser.
65+
66+
#### Code Quality
67+
68+
```bash
69+
# Check formatting
70+
npm run format:check
71+
72+
# Fix formatting
73+
npm run format:fix
74+
75+
# Check linting
76+
npm run lint:check
77+
78+
# Fix linting issues
79+
npm run lint:fix
80+
81+
# Fix all formatting and linting issues
82+
npm run fix
83+
```
84+
4085
### Usage
4186

4287
**Install package** as devDependency (it is practical if the package is part of the project so that you always receive updates)

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default [
1818
ignores: [
1919
'**/.DS_Store',
2020
'**/node_modules',
21+
'**/coverage',
2122
'**/bin',
2223
'**/dist',
2324
'**/demo',

0 commit comments

Comments
 (0)