tests — doctor

Module: tests-doctor Cohesion: 0.80 Members: 0

tests — doctor

This document describes the tests/doctor/doctor.test.ts module, which is responsible for verifying the functionality and output of the core doctor health check system.

Module: tests/doctor/doctor.test.ts

Purpose

The doctor.test.ts module serves as the primary test suite for the src/doctor module. Its main goal is to ensure that the runDoctorChecks function correctly identifies system health issues, reports accurate statuses (ok, warn, error), and provides meaningful messages for various environmental checks.

This test suite is crucial for maintaining the reliability of the doctor command, ensuring it provides consistent and actionable feedback to developers about their local environment setup.

Overview

The test suite operates by executing the runDoctorChecks function once before all tests. The results, an array of DoctorCheck objects, are then used across multiple individual test cases (it blocks) to assert specific properties and outcomes. This approach ensures efficiency by avoiding redundant execution of the potentially resource-intensive doctor checks.

Test Setup (beforeAll)

Before any individual test case runs, the beforeAll hook is used to invoke the runDoctorChecks function. This function is called with process.cwd(), meaning the doctor checks are performed against the current working directory where the tests are executed. The returned array of DoctorCheck objects is stored in a checks variable, making it accessible to all subsequent it blocks within the describe suite.

graph TD
    A[describe('Doctor')] --> B(beforeAll);
    B --> C{runDoctorChecks(process.cwd())};
    C --> D[checks = DoctorCheck[]];
    D --> E[it('should return array')];
    D --> F[it('should have valid status')];
    D --> G[it('should pass Node.js')];
    D --> H[it('should detect git')];
    D --> I[it('should check API keys')];
    D --> J[it('should check disk space')];

Test Cases

The doctor.test.ts module includes several specific test cases to validate different aspects of the doctor module's output.

1. General Result Structure

it('should return an array of checks', () => { /* ... */ });
it('should have valid status values for all checks', () => { /* ... */ });

These tests verify the fundamental structure and content of the runDoctorChecks output:

2. Node.js Version Check

it('should pass Node.js version check', () => { /* ... */ });

This test specifically targets the "Node.js version" check. It expects this check to exist within the results and to have an 'ok' status, assuming the test environment's Node.js version meets the requirements.

3. Git Repository Check

it('should detect git in a git repo', () => { /* ... */ });

This test verifies the "Git" check. It expects the check to be present and its status to be either 'ok' or 'warn'.

This conditional assertion accounts for the fact that the test environment itself might or might not be a Git repository.

4. API Key Checks

it('should check API keys', () => { /* ... */ });

This test focuses on the API key checks. It filters the results for checks whose names start with "API key:" and asserts that exactly four such checks are found. This ensures that all expected API key checks (e.g., for different services) are being performed and reported.

5. Disk Space Check

it('should check disk space', () => { /* ... */ });

This test verifies the "Disk space" check. It expects this check to be present and its status to be either 'ok' or 'warn', reflecting the current disk space availability of the test environment.

Key Takeaways for Developers