Registry / http-networking / httpception

httpception

JSON →
library4.0.1jsnpmunverified

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.

npm install httpception
INSTALL
IMPORT
SIG · HTTPCEPTION
H
httpception
http-networkingjavascriptv4.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
const httpception = require('httpception');
import httpception from 'httpception';
Package uses CommonJS; default export is a function.
default (ESM)
import httpception from 'httpception';
import * as httpception from 'httpception';
ESM import of default works, but use of named exports is unsupported.
default (TypeScript)
import httpception from 'httpception';
import * as httpception from 'httpception';
TypeScript treats it as a default export; enable esModuleInterop for proper CJS interop.

Demonstrates basic HTTP mocking with automatic cleanup via afterEach block.

const httpception = require('httpception'); const got = require('got'); const assert = require('assert'); httpception({ request: 'GET http://example.com/foobar', response: { statusCode: 200, headers: { 'Content-Type': 'text/plain', }, body: 'the text', }, }); get('example.com/foobar').then((response) => { assert.strictEqual(response.body, 'the text'); });
Debug
Known issues
gotchaIf the registered afterEach fails due to unexercised mocks, it can cause test framework issues.
fix
Use the callback form to scope mocks explicitly: httpception({...}, () => { ... })
affects: >=0.0.0
breakingVersion 3.x dropped support for Node <6.0.0.
fix
Upgrade Node to >=6.0.0 or stay on v2.x.
affects: >=3.0.0 <4.0.0
gotchaMocked requests must match exactly including query parameters.
fix
Use a function as request to ignore certain parts: request: { url: '/foo', method: 'GET', query: { bar: 'baz' } }
affects: >=0.0.0
gotchaMocks are not removed if an error is thrown synchronously before returning from the callback.
fix
Ensure the callback returns the promise or handle errors properly.
affects: >=0.0.0
Errors
Common errors & fixes
Error: No mock matched for GET http://example.com/notfound
The request URL does not match any defined mock.
fix
Check the request URL and method; ensure the mock definition is correct.
AssertionError: expected false to be truthy
AfterEach callback detected unexercised mocks.
fix
Ensure all mocks are exercised during the test, or use callback form.
TypeError: httpception is not a function
Incorrect import (e.g., using named import instead of default).
fix
Use require('httpception') or default import.
Upgrade
Version history
4.0.1latest on npm
Audit
Dependencies
raw-bodyoptionalUsed to parse response body if body is a Buffer or string.
mixed-fetchoptionalMay be used for fetch mocking; not always present.
Agent activity
4 hits · last 30 days
node
4
Resources
httpception — npm install httpception · libregistry