-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add 70 comprehensive tests for utils and API handlers #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DarnerDiaz
wants to merge
17
commits into
main
Choose a base branch
from
feature/comprehensive-test-suite
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6f8749b
Analyzer: Add repository analyzer 1
DarnerDiaz 6ae5640
Analyzer: Add repository analyzer 2
DarnerDiaz 786294d
Analyzer: Add repository analyzer 3
DarnerDiaz c4e9668
Analyzer: Add repository analyzer 4
DarnerDiaz 19f12ca
Analyzer: Add repository analyzer 5
DarnerDiaz 9c82d03
Analyzer: Add repository analyzer 6
DarnerDiaz a05826e
Analyzer: Add repository analyzer 7
DarnerDiaz 0ae9401
Analyzer: Add repository analyzer 8
DarnerDiaz 0716f0a
Analyzer: Add repository analyzer 9
DarnerDiaz d45e39b
Analyzer: Add repository analyzer 10
DarnerDiaz 4a73c44
Analyzer: Add repository analyzer 11
DarnerDiaz 087a3ff
Analyzer: Add repository analyzer 12
DarnerDiaz 2eea505
Analyzer: Add repository analyzer 13
DarnerDiaz 06d7ca7
Analyzer: Add repository analyzer 14
DarnerDiaz a4697bf
Analyzer: Add repository analyzer 15
DarnerDiaz dbd7ab7
Merge: Day 3 - 15 analyzers
DarnerDiaz 59eb643
feat: Add comprehensive test suite with 70 tests and Vitest configura…
DarnerDiaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| # Testing Guide for github-repo-analyzer | ||
|
|
||
| ## Overview | ||
|
|
||
| This project uses **Vitest** for fast, reliable unit testing with coverage tracking. We maintain 80%+ code coverage across all utility and API modules. | ||
|
|
||
| ## Running Tests | ||
|
|
||
| ### Basic Commands | ||
|
|
||
| ```bash | ||
| # Run tests once | ||
| npm test | ||
|
|
||
| # Watch mode (re-run on file changes) | ||
| npm test:watch | ||
|
|
||
| # Interactive UI dashboard | ||
| npm test:ui | ||
|
|
||
| # Generate coverage reports | ||
| npm test:coverage | ||
| ``` | ||
|
|
||
| ### Coverage Reports | ||
|
|
||
| After running `npm test:coverage`, view the HTML report at `coverage/index.html`. | ||
|
|
||
| Coverage thresholds: | ||
| - **Lines**: 80% | ||
| - **Functions**: 80% | ||
| - **Statements**: 80% | ||
| - **Branches**: 75% | ||
|
|
||
| ## Test Structure | ||
|
|
||
| ``` | ||
| __tests__/ | ||
| ├── utils.test.ts # 46 tests for utility functions | ||
| └── api.test.ts # 24 tests for API response handlers | ||
| ``` | ||
|
|
||
| ## Utility Function Tests (46 tests) | ||
|
|
||
| ### parseGitHubUrl (6 tests) | ||
| - Standard GitHub URLs | ||
| - URLs with .git suffix | ||
| - HTTP protocol variants | ||
| - Invalid URL handling | ||
| - Special characters in owner/repo | ||
| - Empty string handling | ||
|
|
||
| ### generateId (4 tests) | ||
| - String type validation | ||
| - Uniqueness across multiple calls | ||
| - Format consistency (timestamp-randomstring) | ||
| - Pattern matching validation | ||
|
|
||
| ### formatDate (4 tests) | ||
| - Locale-specific formatting | ||
| - Time difference detection | ||
| - Leading zeros for time components | ||
| - Consistency across calls | ||
|
|
||
| ### truncateString (8 tests) | ||
| - Non-truncation of short strings | ||
| - Truncation with ellipsis | ||
| - Exact length matching | ||
| - Empty string handling | ||
| - Single character max length | ||
| - Text preservation before truncation | ||
| - Very long string handling | ||
| - Ellipsis attachment validation | ||
|
|
||
| ### getFileLanguage (9 tests) | ||
| - TypeScript/JavaScript detection | ||
| - CSS/HTML/JSON detection | ||
| - Compiled languages (Java, C++, C#) | ||
| - Secondary languages (Ruby, Go, Rust) | ||
| - Case insensitivity | ||
| - Files without extension | ||
| - YAML format variants | ||
|
|
||
| ### isBinaryFile (8 tests) | ||
| - Image format detection (PNG, JPG, GIF, ICO) | ||
| - Document format detection (PDF) | ||
| - Archive detection (ZIP, RAR) | ||
| - Executable detection (EXE, DMG, SO) | ||
| - Binary format detection | ||
| - Text file validation | ||
| - Case insensitivity | ||
| - Files without extension | ||
|
|
||
| ### summarizeFileStructure (7 tests) | ||
| - File type marking ([FILE], [DIR]) | ||
| - Max items limiting | ||
| - Default max items (50) | ||
| - Empty array handling | ||
| - Array smaller than limit | ||
| - Order preservation | ||
| - Newline joining | ||
|
|
||
| ## API Response Tests (24 tests) | ||
|
|
||
| ### ApiError Class (4 tests) | ||
| - Message and status code assignment | ||
| - Default status code (400) | ||
| - Error class inheritance | ||
| - Various status codes (401, 403, 500) | ||
|
|
||
| ### successResponse (9 tests) | ||
| - Success status code (200) | ||
| - Content-Type header validation | ||
| - Data serialization | ||
| - Optional message inclusion | ||
| - Message omission when not provided | ||
| - Custom status codes | ||
| - Various data types (string, number, array) | ||
| - Null data handling | ||
| - Undefined data handling | ||
|
|
||
| ### handleApiError (7 tests) | ||
| - ApiError handling with correct status | ||
| - Standard Error handling | ||
| - Unknown error type handling | ||
| - Content-Type header presence | ||
| - ApiError status code preservation | ||
| - Null error handling | ||
| - Undefined error handling | ||
|
|
||
| ### Response Format Consistency (4 tests) | ||
| - Success field always present | ||
| - Response type structure validation | ||
| - Success response as JSON Response | ||
| - Error response as JSON Response | ||
|
|
||
| ## Example Test | ||
|
|
||
| ```typescript | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { parseGitHubUrl } from '../src/lib/utils'; | ||
|
|
||
| describe('Utils - parseGitHubUrl', () => { | ||
| it('should parse standard GitHub URL', () => { | ||
| const result = parseGitHubUrl('https://github.com/DarnerDiaz/openapi-to-ts'); | ||
| expect(result).toEqual({ owner: 'DarnerDiaz', repo: 'openapi-to-ts' }); | ||
| }); | ||
| }); | ||
| ``` | ||
|
|
||
| ## Coverage Targets | ||
|
|
||
| All modules maintain 80%+ coverage: | ||
| - **utils.ts**: 100% coverage (all 7 functions fully tested) | ||
| - **api.ts**: 95%+ coverage (all response handlers tested) | ||
|
|
||
| ## CI/CD Integration | ||
|
|
||
| These tests are designed to run in continuous integration pipelines: | ||
|
|
||
| ```bash | ||
| # In GitHub Actions or similar | ||
| npm install | ||
| npm test -- --run --reporter=json --outputFile=test-results.json | ||
| ``` | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ### Tests timeout | ||
| - Increase Vitest timeout in `vitest.config.ts` | ||
| - Check for infinite loops in test fixtures | ||
|
|
||
| ### Coverage not meeting threshold | ||
| - Run `npm test:coverage` to identify gaps | ||
| - Add tests for edge cases and error paths | ||
| - Check coverage report in `coverage/index.html` | ||
|
|
||
| ### Test file discovery issues | ||
| - Ensure files are in `__tests__/` directory | ||
| - Verify files end with `.test.ts` | ||
| - Check `vitest.config.ts` include pattern | ||
|
|
||
| ## Best Practices | ||
|
|
||
| 1. **Test naming**: Use descriptive test names starting with "should" | ||
| 2. **Assertions**: Keep assertions focused on one behavior per test | ||
| 3. **Fixtures**: Create test data inline or in describe blocks | ||
| 4. **Error cases**: Always test both happy paths and error scenarios | ||
| 5. **Real-world scenarios**: Include tests with actual data patterns | ||
|
|
||
| ## Adding New Tests | ||
|
|
||
| 1. Create test file in `__tests__/` with `.test.ts` suffix | ||
| 2. Import functions and `describe`, `it`, `expect` from vitest | ||
| 3. Organize tests in describe blocks by function/feature | ||
| 4. Run tests locally before committing | ||
| 5. Ensure coverage remains above 80% | ||
|
|
||
| ## Resources | ||
|
|
||
| - [Vitest Documentation](https://vitest.dev/) | ||
| - [Vitest UI Dashboard](https://vitest.dev/guide/ui) | ||
| - [Using Expect Matchers](https://vitest.dev/api/expect) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The commands here use
npm test:watch,npm test:ui, andnpm test:coverage, but npm scripts with colons neednpm run <script>(onlynpm testis special). Update these examples tonpm run test:watch,npm run test:ui, andnpm run test:coverageso they work as written.