Skip to content

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.

We use Vitest for unit testing in the frontend (Svelte 5).

To run unit tests locally, navigate to the frontend-pwa/ directory and use:

Terminal window
npm run test

Code coverage is measured using @vitest/coverage-v8. You can generate a coverage report locally by running:

Terminal window
npm run test -- --coverage

This will generate an HTML report in frontend-pwa/coverage/index.html.

Our GitHub Actions workflow (CI: Frontend) automatically runs unit tests on every push and pull request to main and develop branches.

We have integrated vitest-coverage-report-action to provide immediate feedback on code coverage changes. When a Pull Request is created or updated:

  1. The CI runner executes tests with coverage enabled.
  2. A summary of the coverage report is posted as a comment on the PR.
  3. The report includes a comparison between the current branch and the base branch, highlighting improvements or regressions.

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-pwa
  • 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.