The `test` command in Hapi CLI is used to execute tests for your Hapi.js application. While Hapi.js itself is a framework and doesn't ship with a built-in test runner, its ecosystem often relies on tools like Lab (a Hapi-specific test utility), Jest, Mocha, or others. The `hapi test` command, if implemented in a hypothetical Hapi CLI, would likely act as a wrapper to run these tests, providing convenience and potentially integrating with Hapi-specific configurations.
hapi test [options]* `--runner <runner>`: Specifies the test runner to use (e.g., `lab`, `jest`, `mocha`). If omitted, it might default to a configured runner or try to detect one.
* `--watch`: Runs tests in watch mode, re-running them whenever relevant files change.
* `--files <glob>`: Specifies a glob pattern for test files to run (e.g., `./test/**/*.spec.js`).
* `--coverage`: Generates a test coverage report.
* `--grep <pattern>`: Runs only tests matching the given pattern (e.g., a test title).
* `--reporter <reporter>`: Specifies the test reporter to use (e.g., `spec`, `html`, `json`).
* `--env <env>`: Sets the `NODE_ENV` environment variable for the test run (e.g., `test`).
* `--config <path>`: Specifies a path to a custom test configuration file.
**1. Run all tests with the default runner (e.g., Lab if configured):**
hapi testThis command would typically scan your project for test files (often in a `test/` directory) and execute them using the primary test runner configured for your Hapi.js project.
**2. Run tests using a specific runner (e.g., Jest):**
hapi test --runner jestIf your project uses Jest for testing, this command explicitly tells the Hapi CLI to invoke Jest to run your tests. It might leverage a `jest.config.js` file if present.
**3. Run tests in watch mode:**
hapi test --watchUseful during development, this command will keep the test runner active and automatically re-execute tests whenever you save changes to your source code or test files.
**4. Run tests and generate a coverage report:**
hapi test --coverageThis will execute your tests and, upon completion, generate a detailed code coverage report, typically showing which parts of your code are covered by tests and which are not.
**5. Run specific test files:**
hapi test --files "./test/routes/*.spec.js"If you only want to run tests for a specific part of your application, you can provide a glob pattern to filter the test files.
**6. Run tests matching a specific pattern (e.g., a test description):**
hapi test --grep "user authentication"This is useful when debugging or focusing on a particular feature. It will only run tests whose titles or descriptions contain the specified pattern.
The `hapi test` command serves as a convenient entry point for developers to interact with their application's test suite. While the actual test execution logic is delegated to underlying test frameworks (like Lab, Jest, Mocha, or others), the Hapi CLI provides a unified interface. It abstracts away the direct invocation of these test runners and their sometimes complex command-line arguments, offering a more consistent and Hapi-centric testing experience.
Its main advantages would be:
* **Consistency**: Provides a single command regardless of the underlying test runner.
* **Configuration**: Can integrate with project-level test configurations defined within the Hapi CLI or specific Hapi.js project settings.
* **Convenience**: Offers common testing options (watch, coverage, filtering) through standardized flags.