http-request-mock is a versatile JavaScript/TypeScript library designed for intercepting and mocking HTTP requests across various environments and client libraries. It operates at a low level, directly hooking into XMLHttpRequest, fetch, and Node.js's native http/https modules, allowing it to mock requests from popular libraries like Axios, jQuery, Superagent, Ky, Node-Fetch, and Got without specific integrations for each. The current stable version is 2.0.2. The library aims to provide a 'non-hacking' approach to mocking by offering a webpack plugin and a command-line tool, enabling separation of mock data from business logic and eliminating the need for code modifications during development or testing. Its core differentiator is this low-level, environment-agnostic interception combined with tooling for externalizing mock configurations, which distinguishes it from libraries that require proxy setups or direct code alterations.
npm install http-request-mockVerified import paths — ran on the pinned version, not inferred.
Sets up `http-request-mock` to intercept and respond to GET and POST requests for a `/api/users` endpoint, showcasing dynamic responses, status codes, and network delays. It then simulates client-side `fetch` calls to trigger these mocked behaviors.
Consult the official documentation for v2.0.0+ to understand specific API changes, migration paths, and ensure compatibility with your existing mock configurations.
Ensure `mock.setup()` is called as early as possible in your application or test suite. Use `mock.teardown()` consistently, especially in `afterEach` or `afterAll` hooks in testing frameworks, to isolate mock effects and prevent leaks between tests.
Thoroughly review the specific documentation for the Webpack plugin or CLI tool. Verify your `webpack.config.js` or CLI arguments correctly define mock file locations and options, especially in projects with complex build processes.
Ensure you are importing `mock` as `import mock from 'http-request-mock';` and that `mock.setup()` is invoked before attempting to define any request mocks (e.g., `mock.get()`, `mock.post()`).
Verify `mock.setup()` is called and executed. Carefully check the URL patterns (including query parameters, if applicable) and HTTP methods in your mock definitions to ensure they align exactly with the actual requests being made by your application.
Ensure `http-request-mock.setup()` is invoked as early as possible in your Node.js application or test environment, ideally before any other modules that might modify native HTTP methods are loaded or initialized. Review module loading order if conflicts persist.
No dependency data recorded yet.