Testing & Code Coverage
Testing & Code Coverage
Section titled “Testing & Code Coverage”Dice Chess Trainer uses a robust testing strategy to ensure reliability and maintainability. This document describes the tools and processes used for unit testing and code coverage reporting.
Unit Testing
Section titled “Unit Testing”We use Vitest for unit testing in the frontend (Svelte 5).
Running Tests Locally
Section titled “Running Tests Locally”To run unit tests locally, navigate to the frontend-pwa/ directory and use:
npm run testCoverage Reports
Section titled “Coverage Reports”Code coverage is measured using @vitest/coverage-v8. You can generate a coverage report locally by running:
npm run test -- --coverageThis will generate an HTML report in frontend-pwa/coverage/index.html.
Continuous Integration (CI)
Section titled “Continuous Integration (CI)”Our GitHub Actions workflow (CI: Frontend) automatically runs unit tests on every push and pull request to main and develop branches.
Automated PR Comments
Section titled “Automated PR Comments”We have integrated vitest-coverage-report-action to provide immediate feedback on code coverage changes. When a Pull Request is created or updated:
- The CI runner executes tests with coverage enabled.
- A summary of the coverage report is posted as a comment on the PR.
- The report includes a comparison between the current branch and the base branch, highlighting improvements or regressions.
Configuration
Section titled “Configuration”The coverage reporting is configured in frontend-pwa/vitest.config.ts:
coverage: { reporter: ['text', 'json', 'json-summary', 'html'], include: ['src/**'], exclude: [ 'src/main.ts', 'src/vite-env.d.ts', '**/types.ts', '**/*.test.ts', '**/fixtures/**' ]}And in .github/workflows/frontend-ci.yaml:
- name: Run unit tests with Vitest (with coverage) run: npm run test -- --run --coverage
- name: Report coverage if: always() uses: davelosert/vitest-coverage-report-action@v2 with: working-directory: ./frontend-pwaBest Practices
Section titled “Best Practices”- Aim for High Coverage: We target at least 80% coverage for core domain logic and state management.
- Test User Interactions: Use
jsdom(already configured) to test Svelte components and user events. - Mock External APIs: Use Vitest’s mocking capabilities to isolate unit tests from backend services.
- Check PR Comments: Always review the coverage report in your PR to ensure you haven’t introduced untested code.