{"id":18440,"library":"httpception","title":"httpception","description":"Version 4.0.1. A lightweight HTTP mocking library for Node.js that intercepts HTTP traffic during tests. It automatically restores the http module after each test (via a registered afterEach block) and fails if any mocks were not exercised. Supports both simple inline mocking and a callback-based approach for scoping. Differentiates from alternatives like nock by its automatic cleanup and integration with test frameworks.","status":"active","version":"4.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/papandreou/httpception","tags":["javascript"],"install":[{"cmd":"npm install httpception","lang":"bash","label":"npm"},{"cmd":"yarn add httpception","lang":"bash","label":"yarn"},{"cmd":"pnpm add httpception","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used to parse response body if body is a Buffer or string.","package":"raw-body","optional":true},{"reason":"May be used for fetch mocking; not always present.","package":"mixed-fetch","optional":true}],"imports":[{"note":"Package uses CommonJS; default export is a function.","wrong":"import httpception from 'httpception';","symbol":"default","correct":"const httpception = require('httpception');"},{"note":"ESM import of default works, but use of named exports is unsupported.","wrong":"import * as httpception from 'httpception';","symbol":"default (ESM)","correct":"import httpception from 'httpception';"},{"note":"TypeScript treats it as a default export; enable esModuleInterop for proper CJS interop.","wrong":"import * as httpception from 'httpception';","symbol":"default (TypeScript)","correct":"import httpception from 'httpception';"}],"quickstart":{"code":"const httpception = require('httpception');\nconst got = require('got');\nconst assert = require('assert');\n\nhttpception({\n  request: 'GET http://example.com/foobar',\n  response: {\n    statusCode: 200,\n    headers: {\n      'Content-Type': 'text/plain',\n    },\n    body: 'the text',\n  },\n});\n\nget('example.com/foobar').then((response) => {\n  assert.strictEqual(response.body, 'the text');\n});","lang":"javascript","description":"Demonstrates basic HTTP mocking with automatic cleanup via afterEach block."},"warnings":[{"fix":"Use the callback form to scope mocks explicitly: httpception({...}, () => { ... })","message":"If the registered afterEach fails due to unexercised mocks, it can cause test framework issues.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Upgrade Node to >=6.0.0 or stay on v2.x.","message":"Version 3.x dropped support for Node <6.0.0.","severity":"breaking","affected_versions":">=3.0.0 <4.0.0"},{"fix":"Use a function as request to ignore certain parts: request: { url: '/foo', method: 'GET', query: { bar: 'baz' } }","message":"Mocked requests must match exactly including query parameters.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Ensure the callback returns the promise or handle errors properly.","message":"Mocks are not removed if an error is thrown synchronously before returning from the callback.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Check the request URL and method; ensure the mock definition is correct.","cause":"The request URL does not match any defined mock.","error":"Error: No mock matched for GET http://example.com/notfound"},{"fix":"Ensure all mocks are exercised during the test, or use callback form.","cause":"AfterEach callback detected unexercised mocks.","error":"AssertionError: expected false to be truthy"},{"fix":"Use require('httpception') or default import.","cause":"Incorrect import (e.g., using named import instead of default).","error":"TypeError: httpception is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}