CLI Companion

  • Hugging Face CLI
    • login
    • whoami
    • repo create
    • upload
    • download
    • lfs-enable-largefiles
    • scan-cache
    • delete-cache
  • Hapi CLI
    • new
    • start
    • build
    • test
    • plugin create
    • route add
  • Cloudflared
    • tunnel
    • tunnel run
    • tunnel list
    • tunnel delete
    • access
    • access tcp
    • update

    The `hapi test` command is a versatile utility within the Hapi CLI, designed to help developers ensure the quality and stability of their Hapi applications. It can be used to run various types of tests (unit, integration, E2E), perform code linting, and even check the health of a running Hapi server. This command integrates seamlessly with common testing frameworks and allows for flexible configuration and execution.

    Syntax

    bash
    hapi test [options]

    Options

    * `--unit`: Runs only unit tests. These are typically fast tests focused on individual functions or modules.

    * `--integration`: Runs only integration tests. These tests verify the interaction between different components or services.

    * `--e2e`: Runs only end-to-end tests. These simulate user scenarios to test the entire application flow.

    * `--lint`: Performs code linting based on the project's configured linting rules (e.g., ESLint). This checks for style violations and potential errors without running functional tests.

    * `--health`: Checks the health of a running Hapi server. This typically pings a configured health endpoint (e.g., `/health`) and reports its status.

    * `--watch`: Enables watch mode. The CLI will monitor file changes in the project and automatically re-run relevant tests or linting upon detection.

    * `--config <path>`: Specifies a custom test configuration file. By default, `hapi test` looks for `test.config.js` or similar standard configurations.

    * `--grep <pattern>`: Filters tests to run only those whose names match the provided pattern (case-sensitive regex).

    * `--reporter <type>`: Specifies the test report format (e.g., `spec`, `dot`, `json`, `html`). Default is usually `spec`.

    * `--verbose`: Increases the verbosity of the output, providing more detailed logs during test execution.

    Usage Examples

    1. **Run all configured tests (unit, integration, and E2E):**

    bash
    hapi test

    This command will execute all test suites defined in your project's default test configuration.

    2. **Run only unit tests:**

    bash
    hapi test --unit

    Useful for quick feedback during development, focusing on isolated code components.

    3. **Run only integration tests and watch for changes:**

    bash
    hapi test --integration --watch

    This will run your integration tests initially and then re-run them whenever relevant source or test files are modified.

    4. **Perform code linting:**

    bash
    hapi test --lint

    Checks your codebase against defined linting rules without executing any functional tests.

    5. **Check the health of a running Hapi server:**

    bash
    hapi test --health

    Useful for deployment pipelines or monitoring to quickly ascertain the operational status of your Hapi application.

    6. **Run E2E tests with a specific configuration file:**

    bash
    hapi test --e2e --config ./config/e2e-ci.js

    Allows you to override the default test configuration with one tailored for specific environments, like a CI/CD pipeline.

    7. **Run unit tests matching a specific pattern and output as JSON:**

    bash
    hapi test --unit --grep "User API" --reporter json

    This command will execute only unit tests whose descriptions or names contain "User API" and output the results in JSON format, which can be useful for programmatic consumption or reporting tools.

    8. **Run all tests with verbose output:**

    bash
    hapi test --verbose

    Provides more detailed information about the test execution process, including individual test outcomes and setup/teardown phases.

    The `hapi test` command provides a robust and flexible way to manage and execute tests, ensuring that Hapi applications maintain high quality throughout their lifecycle.