{"id":17679,"library":"hock","title":"Hock: HTTP Mocking Server","description":"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.","status":"abandoned","version":"1.4.1","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/mmalecki/hock","tags":["javascript","mock","http","test"],"install":[{"cmd":"npm install hock","lang":"bash","label":"npm"},{"cmd":"yarn add hock","lang":"bash","label":"yarn"},{"cmd":"pnpm add hock","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used internally by hock for making requests. This package is deprecated and unmaintained, posing potential security risks and compatibility issues with modern Node.js.","package":"request","optional":false},{"reason":"Utility for debugging output.","package":"debug","optional":false},{"reason":"General utility belt functions.","package":"lodash","optional":false}],"imports":[{"note":"Hock is a CommonJS module and must be imported using `require()`.","wrong":"import hock from 'hock';","symbol":"hock","correct":"const hock = require('hock');"},{"note":"The `createHock` function is a method of the default `hock` export object, not a named export. It is accessed directly from the imported `hock` object.","wrong":"import { createHock } from 'hock';","symbol":"createHock","correct":"const hock = require('hock');\nconst mockServer = hock.createHock();"},{"note":"Hock does not ship with TypeScript type definitions. Any type annotations for `HockServer` or other internal types would need to be manually defined or sourced from community-contributed types (if any exist).","symbol":"HockServer (interface)","correct":"/** @typedef {import('hock').HockServer} HockServer */"}],"quickstart":{"code":"const http = require('http');\nconst hock = require('hock');\nconst request = require('request'); // NOTE: 'request' library is deprecated\n\nconst mockPort = 1337;\nconst mock = hock.createHock();\n\n// Define a mock expectation: a GET request to '/some/url' should return 'Hello!'\nmock\n    .get('/some/url')\n    .reply(200, 'Hello!', { 'Content-Type': 'text/plain' });\n\nmock\n    .post('/data', { foo: 'bar' })\n    .reply(201, { status: 'created' }, { 'Content-Type': 'application/json' });\n\n// Create an HTTP server that uses hock's handler\nconst server = http.createServer(mock.handler);\n\nserver.listen(mockPort, () => {\n    console.log(`Mock server listening on http://localhost:${mockPort}`);\n\n    // Make an HTTP request to the mock server for GET /some/url\n    request(`http://localhost:${mockPort}/some/url`, (err, res, body) => {\n        if (err) { console.error('GET Request failed:', err); return; }\n        console.log(`GET /some/url: Status ${res.statusCode}, Body: ${body}`);\n    });\n\n    // Make an HTTP request to the mock server for POST /data\n    request.post({\n        url: `http://localhost:${mockPort}/data`,\n        json: { foo: 'bar' }\n    }, (err, res, body) => {\n        if (err) { console.error('POST Request failed:', err); return; }\n        console.log(`POST /data: Status ${res.statusCode}, Body: ${JSON.stringify(body)}`);\n\n        // Verify all mocks are satisfied and close the server\n        if (mock.remainingExpectations === 0) {\n            console.log('All mock expectations satisfied.');\n        } else {\n            console.warn(`${mock.remainingExpectations} mock expectations remain unsatisfied.`);\n        }\n        server.close(() => { console.log('Mock server closed.'); });\n    });\n});\n\nserver.on('error', (e) => { console.error('Server error:', e); });","lang":"javascript","description":"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."},"warnings":[{"fix":"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.","message":"The `hock` package is unmaintained, with its last release over eight years ago. It relies on the deprecated `request` library, which is no longer actively developed and may have security vulnerabilities or compatibility issues with modern Node.js environments.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Always use `const hock = require('hock');` to import the library in your Node.js projects.","message":"Hock is a CommonJS module and does not natively support ES Modules (`import/export` syntax). Attempting to import it using ES Module syntax will result in runtime errors.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Thoroughly test `hock`'s behavior if used with Node.js versions above 10.x. For new projects, prioritize modern, actively maintained HTTP mocking libraries.","message":"The package targets Node.js versions `>=0.8.x`, which are extremely old and no longer supported. Running `hock` on modern Node.js versions might encounter compatibility problems, unexpected behavior, or silent failures due to changes in core Node.js APIs.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"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')`).","cause":"Attempting to use `hock` (a CommonJS module) in an ES Modules context (e.g., in a file where `type: module` is set in `package.json` or a `.mjs` file).","error":"require is not defined"},{"fix":"The `createHock` function is a method of the default `hock` export object. Access it via `const hock = require('hock'); const mock = hock.createHock();`","cause":"Incorrectly trying to destructure `createHock` from the `hock` module, e.g., `const { createHock } = require('hock');`.","error":"TypeError: hock.createHock is not a function"},{"fix":"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.","cause":"Older Node.js versions or libraries (like the deprecated `request` used by `hock`) may have issues with modern SSL certificates and cipher suites, especially when making requests to HTTPS endpoints that use newer security standards.","error":"Error: unable to verify the first certificate"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}