Registry / http-networking / think-mock-http

think-mock-http

JSON →
library1.0.6jsnpmunverified

The `think-mock-http` package provides a dedicated utility for simulating HTTP requests and responses within ThinkJS 3.x applications, primarily for testing purposes. Currently at version 1.0.6, the library offers a simple API to create mock contexts, allowing developers to test controllers, middleware, and services without requiring a live HTTP server. Given its last significant update in 2019, its release cadence is slow, existing mainly for maintenance within the ThinkJS ecosystem rather than active feature development. Its key differentiator is its deep integration with the ThinkJS core, ensuring compatibility with the framework's internal request/response lifecycle, unlike generic HTTP mocking libraries.

npm install think-mock-http
INSTALL
IMPORT
SIG · THINK-MOCK-HTTP
T
think-mock-http
http-networkingjavascriptv1.0.6
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.

mockHttp
import mockHttp from 'think-mock-http';
import { mockHttp } from 'think-mock-http';
The library primarily uses CommonJS `module.exports = mockHttp`, which resolves as a default import in ESM. Avoid named imports.
mockHttp
const mockHttp = require('think-mock-http');
Standard CommonJS import pattern for Node.js environments.
MockHttpFunction (JSDoc type)
/** @typedef {import('think-mock-http')} MockHttpFunction */
For type hinting with JSDoc in JavaScript projects, or when referencing the function type in TypeScript without explicit declaration files.

Demonstrates how to create a mock HTTP request context for a ThinkJS application, pass it a stubbed ThinkJS instance, and retrieve the simulated response for testing controller logic.

import mockHttp from 'think-mock-http'; // A minimal stub for the 'think' object for demonstration purposes. // In a real ThinkJS test, 'think' would be the actual ThinkJS instance // provided by the ThinkJS test environment. const mockThink = { // Simulate common ThinkJS methods or properties that think-mock-http might interact with config: (key) => { if (key === 'url_pathname_prefix') return ''; return {}; }, app: { emit: (event, ...args) => console.log(`ThinkJS app event: ${event}`, args) }, Logger: class MockLogger { log(...args) { console.log('Mock Logger:', ...args); } }, Controller: class MockController {}, Service: class MockService {}, }; async function runMockHttpRequest() { try { const http = await mockHttp(mockThink, { // Pass the ThinkJS instance (or stub) url: '/api/users/123?param=test', // The URL path to mock method: 'GET', // 'data' is typically for POST/PUT, but can be included for completeness data: { query: 'example' }, header: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${process.env.TEST_AUTH_TOKEN ?? 'mock-token'}`, // Use env vars for secrets 'User-Agent': 'MockTestAgent/1.0', }, referrer: 'http://localhost/', }).run(); // Execute the mock request console.log('--- Mock HTTP Response ---'); console.log('Status:', http.status); console.log('Body:', http.body); console.log('Headers:', http.header); console.log('Redirected:', http.redirected); // Example assertion (in a real test framework like Jest or Mocha) if (http.status === 200 && typeof http.body === 'string' && http.body.includes('Successfully')) { console.log('Mock test successful: Received expected data.'); } else { console.error('Mock test failed: Unexpected response or status.'); } } catch (error) { console.error('Error during mock HTTP request:', error); } } runMockHttpRequest();
Debug
Known issues
gotchaThe package specifies `engines.node: >=6.0.0`, which is an extremely old Node.js version. While it might still run on modern Node.js, it indicates a lack of testing against recent environments and potential incompatibilities with newer JavaScript features or Node.js APIs.
fix
Ensure thorough testing in your target Node.js environment. Consider using nvm or Docker to test against older Node.js versions if issues arise.
affects: >=1.0.0
gotchaThe library exports primarily as CommonJS (`module.exports`). When used in an ES Modules (ESM) context, it must be imported as a default import (`import mockHttp from 'think-mock-http';`). Attempting a named import (`import { mockHttp } from 'think-mock-http';`) will result in an error.
fix
Always use `import mockHttp from 'think-mock-http';` in ESM environments. For CommonJS, use `const mockHttp = require('think-mock-http');`.
affects: >=1.0.0
gotchaThe package's last commit was in 2019, suggesting it is no longer actively developed for new features, only maintained for critical fixes if ThinkJS itself continues to rely on it. This could mean slower resolution of issues or lack of support for future ThinkJS architectural changes.
fix
Review the ThinkJS community and documentation for alternative or newer testing utilities if you encounter unaddressed issues or require features not present in this version. Pin the dependency to avoid unexpected breaking changes if a new major version were to ever be released.
affects: >=1.0.0
gotchaThis library is tightly coupled to the internal structure and API of ThinkJS 3.x. Compatibility with older (ThinkJS 2.x) or potential future major versions of ThinkJS is not guaranteed and would likely require updates to this package.
fix
Always test `think-mock-http` against your specific ThinkJS version. Consult ThinkJS documentation for compatible testing strategies.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: mockHttp is not a function
Attempting to use `mockHttp` after importing it incorrectly, most commonly with a named import (`import { mockHttp } from 'think-mock-http';`) in an ESM context, or if the `require` statement fails in CJS.
fix
Ensure you are using a default import in ESM: `import mockHttp from 'think-mock-http';`. In CommonJS, use `const mockHttp = require('think-mock-http');`.
ReferenceError: think is not defined
The `mockHttp` function requires an instance of the ThinkJS framework (`think` object) as its first argument, which was not provided or was undefined.
fix
When calling `mockHttp(think, { ... })`, ensure that `think` is a properly initialized instance of the ThinkJS application. In a test environment, this `think` object is typically provided by the ThinkJS test runner or needs to be stubbed minimally.
Mocked request does not hit the expected ThinkJS controller/logic, or returns an unexpected response.
Incorrect configuration of the mock request `url`, `method`, `data`, or `header` options, or the stubbed `think` instance is missing critical properties that the ThinkJS routing/middleware expects.
fix
Double-check the `url`, `method`, `data`, and `header` parameters passed to `mockHttp` to accurately reflect the actual request you intend to simulate. Ensure your `think` stub (if not a full ThinkJS instance) provides necessary configurations or methods that `think-mock-http` or your ThinkJS application's routing relies on.
Upgrade
Version history
1.0.6latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
think-mock-http — npm install think-mock-http · libregistry