Skip to content
Merged

Test #50

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ jobs:

- name: Build
run: npm run build

- name: Run Tests
run: npm run test
3 changes: 3 additions & 0 deletions .github/workflows/ci-full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ jobs:
- name: Build
run: npm run build

- name: Run Tests
run: npm run test

tag:
name: Version tag
needs: [check]
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ dist
# TypeScript cache
*.tsbuildinfo

# Test coverage
coverage/

# Optional npm cache directory
.npm

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

## [Unreleased]

### Added

- Unit testing infrastructure using Vitest
- Comprehensive test coverage (~68%) for core modules:
- `commandLine.ts` (84.56%): CLI argument parsing and validation
- `file.ts` (100%): File operations and duplicate detection
- `cppCode.ts` (96.62%): C++ code generation and templates
- `consoleColor.ts` (100%): Console utilities
- Test fixtures for validating file processing
- Coverage reports with HTML output
- Development documentation for testing in README.md and CLAUDE.md

### Changed

- Updated `.gitignore` to exclude `coverage/` directory
- Enhanced documentation with testing sections

## [1.10.0] - 2025-11-20

### Changed
Expand Down
58 changes: 57 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ npm run dev:psychic
# Development with live reload (psychic2 engine)
npm run dev:psychic2

# Run comprehensive tests (requires PlatformIO)
# Run TypeScript unit tests
npm run test

# Run tests in watch mode
npm run test:watch

# Generate test coverage report
npm run test:coverage

# Run comprehensive ESP32 tests (requires PlatformIO)
npm run test:all
```

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

### Testing

The project uses **Vitest** for unit testing with comprehensive coverage:

**Test Structure:**

```
test/
├── unit/
│ ├── commandLine.test.ts # CLI argument parsing tests
│ ├── file.test.ts # File operations tests
│ ├── cppCode.test.ts # C++ code generation tests
│ └── consoleColor.test.ts # Console utilities tests
└── fixtures/
└── sample-files/ # Test fixture files
```

**Test Coverage (68.25% overall):**

- `commandLine.ts`: 84.56% - CLI argument parsing, validation, engine/tri-state validation
- `file.ts`: 100% - File collection, duplicate detection (SHA256), pre-compressed file skipping
- `cppCode.ts`: 96.62% - Template rendering for all 4 engines, etag/gzip combinations, Handlebars helpers
- `cppCodeEspIdf.ts`: 100% - ESP-IDF template (tested via `cppCode.ts`)
- `consoleColor.ts`: 100% - ANSI color code wrapping
- `index.ts`: 0% - Main entry point (has side effects, tested via integration)

**Key Testing Features:**

- **Vitest Configuration**: `vitest.config.ts` with TypeScript support, 60% coverage thresholds
- **Mocking Strategy**: Uses `vi.mock()` for file system (`node:fs`), glob (`tinyglobby`), and module dependencies
- **Dynamic Imports**: Tests use dynamic imports for `commandLine.ts` to test different CLI arguments without side effects
- **Test Fixtures**: Small sample files (HTML/CSS/JS) for testing file processing pipeline
- **Coverage Reports**: Generated in `coverage/` directory (ignored by git), viewable HTML reports at `coverage/index.html`

**Testing Approach:**

- **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.
- **file.test.ts**: Mocks file system with `memfs`, tests duplicate detection, compressed file skipping, path handling
- **cppCode.test.ts**: Tests template selection by engine, code generation for all etag/gzip combinations, byte array conversion, ETag/cache headers, default route detection
- **consoleColor.test.ts**: Simple tests for ANSI escape code wrapping (quick coverage wins)

**Running Tests:**

- `npm run test` - Run all tests once (CI/CD)
- `npm run test:watch` - Watch mode for development
- `npm run test:coverage` - Generate coverage reports

### Demo Projects

- **`demo/svelte/`**: Example Svelte application with Vite, TailwindCSS, gallery images
Expand Down
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,51 @@ This npm package provides a solution for **inserting any JS client application i
- Node.js >= 20
- npm >= 9

### Development

#### Testing

The project includes comprehensive unit tests using Vitest:

```bash
# Run tests once
npm run test

# Run tests in watch mode (for development)
npm run test:watch

# Generate coverage report
npm run test:coverage
```

**Test Coverage:** ~68% overall with focus on core functionality:

- `commandLine.ts`: 84.56% - CLI argument parsing and validation
- `file.ts`: 100% - File operations and duplicate detection
- `cppCode.ts`: 96.62% - C++ code generation and templates
- `consoleColor.ts`: 100% - Console output utilities

Coverage reports are generated in the `coverage/` directory and can be viewed by opening `coverage/index.html` in a browser.

#### Code Quality

```bash
# Check formatting
npm run format:check

# Fix formatting
npm run format:fix

# Check linting
npm run lint:check

# Fix linting issues
npm run lint:fix

# Fix all formatting and linting issues
npm run fix
```

### Usage

**Install package** as devDependency (it is practical if the package is part of the project so that you always receive updates)
Expand Down
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default [
ignores: [
'**/.DS_Store',
'**/node_modules',
'**/coverage',
'**/bin',
'**/dist',
'**/demo',
Expand Down
Loading