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

    `hapi test` command

    The `hapi test` command in the Hapi CLI is used to execute tests for your Hapi.js application or project. It typically acts as a wrapper around a chosen test runner (like Lab, Jest, or Mocha) and provides a convenient way to run your test suite directly from the command line.

    Syntax

    bash
    hapi test [options]

    Description

    When you run `hapi test`, the CLI scans your project for test files (usually found in a `test/` directory or named with conventions like `.test.js` or `.spec.js`) and executes them using the underlying test runner configured for your project. The command supports various options to filter tests, change reporters, set timeouts, and more, providing flexibility in how you manage and run your tests.

    Common Options

    * `-r, --reporter <reporter>`: Specifies the test reporter to use. Common values include `spec`, `dot`, `nyan`, `json`, `xunit`, etc. (availability depends on the underlying test runner).

    * **Example**: `--reporter dot`

    * `-g, --grep <pattern>`: Only runs tests that match the given regular expression pattern. This is useful for running a subset of your tests.

    * **Example**: `--grep "user authentication"`

    * `-i, --ignore <pattern>`: Ignores test files or directories matching the given pattern.

    * **Example**: `--ignore "integration/**"`

    * `-t, --timeout <ms>`: Sets a timeout (in milliseconds) for individual tests or the test suite. If a test exceeds this duration, it will fail.

    * **Example**: `--timeout 5000` (5 seconds)

    * `-p, --path <path>`: Specifies the path to the test files or directory containing them. Defaults to common test directories like `test/`.

    * **Example**: `--path "specs/unit"`

    * `--watch`: Watches for file changes and automatically re-runs tests when relevant files are modified. This is great for development.

    * **Example**: `--watch`

    * `--require <module>`: Requires a specific module before running tests. Useful for setting up test environments, e.g., requiring `@babel/register` for ES6+ syntax.

    * **Example**: `--require @babel/register`

    * `--coverage`: Generates a code coverage report after tests complete. This often requires additional configuration of a coverage tool (e.g., Istanbul).

    * **Example**: `--coverage`

    * `--verbose`: Displays more detailed output during test execution, often including individual test names and their status.

    * **Example**: `--verbose`

    Usage Examples

    1. **Run all tests in your project:**

    bash
    hapi test

    2. **Run tests with the 'dot' reporter:**

    bash
    hapi test --reporter dot

    3. **Run only tests whose descriptions contain "user service":**

    bash
    hapi test --grep "user service"

    4. **Set a timeout of 10 seconds for all tests:**

    bash
    hapi test --timeout 10000

    5. **Watch for file changes and re-run tests automatically:**

    bash
    hapi test --watch

    6. **Run tests in a specific directory `src/api/tests` and generate a coverage report:**

    bash
    hapi test --path "src/api/tests" --coverage

    7. **Combine multiple options (reporter, grep, timeout):**

    bash
    hapi test --reporter nyan --grep "database" --timeout 8000

    Important Considerations

    * **Test Runner Configuration**: The `hapi test` command largely depends on the underlying test runner configured in your Hapi.js project (e.g., via `package.json` scripts or a dedicated test configuration file). The specific options available and their exact behavior might vary based on whether you're using Lab, Jest, Mocha, or another testing framework.

    * **Default Behavior**: By default, `hapi test` will typically discover and run all test files that adhere to common naming conventions (e.g., `*.test.js`, `*.spec.js`) within the standard `test/` directory or subdirectories.

    * **Environment Variables**: You can often control test behavior further using environment variables, which can be particularly useful for continuous integration (CI) environments.