{"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,"search_vec":"'4.0.1':3 'aftereach':29 'altern':54 'approach':49 'automat':18,59 'base':48 'block':30 'callback':47 'callback-bas':46 'cleanup':60 'differenti':52 'exercis':38 'fail':32 'framework':65 'http':6,13,21 'httpception':1 'inlin':42 'integr':62 'intercept':12 'javascript':66 'librari':8 'lightweight':5 'like':55 'mock':7,35,43 'modul':22 'nock':56 'node.js':10 'regist':28 'restor':19 'scope':51 'simpl':41 'support':39 'test':16,25,64 'traffic':14 'version':2 'via':26","created_at":"2026-04-25T07:38:14.588074+00:00","updated_at":"2026-04-25T07:38:14.588074+00:00","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,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/papandreou/httpception","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/httpception","openapi_spec":null,"status_page":null,"smithery":null,"categories":["http-networking","testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-24","install_tag":null}}