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.
hapi test [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.
1. **Run all configured tests (unit, integration, and E2E):**
hapi testThis command will execute all test suites defined in your project's default test configuration.
2. **Run only unit tests:**
hapi test --unitUseful for quick feedback during development, focusing on isolated code components.
3. **Run only integration tests and watch for changes:**
hapi test --integration --watchThis will run your integration tests initially and then re-run them whenever relevant source or test files are modified.
4. **Perform code linting:**
hapi test --lintChecks your codebase against defined linting rules without executing any functional tests.
5. **Check the health of a running Hapi server:**
hapi test --healthUseful for deployment pipelines or monitoring to quickly ascertain the operational status of your Hapi application.
6. **Run E2E tests with a specific configuration file:**
hapi test --e2e --config ./config/e2e-ci.jsAllows 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:**
hapi test --unit --grep "User API" --reporter jsonThis 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:**
hapi test --verboseProvides 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.