Hock is an HTTP mocking server designed for Node.js applications, enabling the definition of mock HTTP requests and their corresponding responses to facilitate testing. Unlike Nock, which intercepts `http.clientRequest`, Hock functions as a self-contained, fully operational HTTP service, making it suitable for integration tests that require a network endpoint to be hit. The package's API is intentionally similar to Nock's, allowing for familiar patterns in defining request expectations and responses. The current stable version is 1.4.1, last updated over eight years ago. The project appears to be unmaintained, evidenced by its reliance on deprecated Node.js versions (>=0.8.x) and older, unmaintained dependencies like `request`. Due to its age and lack of maintenance, it is generally not recommended for new projects or environments running modern Node.js.
npm install hockVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up a `hock` mock server, define expectations for GET and POST requests, send requests to the mock server, and verify that all expectations have been met before shutting down the server.
Consider using actively maintained alternatives like `nock` or a dedicated test server with more recent mock capabilities if possible. If you must use `hock`, be aware of potential security risks and use a specific, pinned version.
Always use `const hock = require('hock');` to import the library in your Node.js projects.Thoroughly test `hock`'s behavior if used with Node.js versions above 10.x. For new projects, prioritize modern, actively maintained HTTP mocking libraries.
Either use CommonJS syntax (`const hock = require('hock');`) in a CommonJS file, or refactor your project to use an ESM-compatible mocking library. If forced to use `hock` in an ESM project, consider dynamic import (`import('hock')`).The `createHock` function is a method of the default `hock` export object. Access it via `const hock = require('hock'); const mock = hock.createHock();`Ensure your environment has up-to-date CA certificates. As a temporary workaround for testing, you might try `process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';` (use with caution, only in development/testing, and understand the security implications), or try downgrading Node.js if compatibility with `hock` is critical and this error is related to Node.js's crypto module.