This library provides a standalone, spec-compliant fake XMLHttpRequest (XHR) object designed specifically for testing browser-based JavaScript libraries. It is partially derived from Sinon.JS but offers a more lightweight alternative for scenarios where a full mocking, spying, or stubbing framework is not required. The current stable version is 2.1.2, last published over five years ago. Its primary function is to allow developers to simulate XHR requests and responses without making actual network calls, which is crucial for deterministic and fast unit testing. Unlike comprehensive testing frameworks, `fake-xml-http-request` intentionally omits mechanisms for auto-swapping the native XHR, recording requests, or playing back interactions, leaving those concerns to the consuming test environment or framework. Its release cadence appears to be infrequent, driven by spec updates or critical bug fixes, rather than a strict schedule.
npm install fake-xml-http-requestVerified import paths — ran on the pinned version, not inferred.
This code demonstrates how to create instances of FakeXMLHttpRequest to simulate successful, erroneous, and aborted network requests, allowing inspection of the XHR object's state and response properties.
Manually instantiate `new FakeXMLHttpRequest()` or integrate with a higher-level mocking library (like Pretender.js or Sinon.JS) that handles global XHR replacement.
For request recording, matching, or more advanced server-side mocking behavior, consider using a library like Pretender.js or Sinon.JS, which often integrate with or build upon such fake XHR implementations.
While functional for many basic use cases, for new projects or those requiring strict spec compliance with the latest XHR features, consider more actively maintained alternatives like `mock-xmlhttprequest` or using built-in mocking features of testing frameworks.
The library provides `FakeXMLHttpRequest` as a class; you must explicitly `import FakeXMLHttpRequest from 'fake-xml-http-request';` and then create instances with `new FakeXMLHttpRequest()`. It does not automatically populate the global `XMLHttpRequest`.
Ensure you are calling `respond` on an instance created by `new FakeXMLHttpRequest()` and that `open()` and `send()` have been called on that instance before attempting to respond.
You must manually create an instance of `FakeXMLHttpRequest` and pass it to the code under test, or temporarily replace the global `window.XMLHttpRequest` with `FakeXMLHttpRequest` in your test setup and tear-down, typically managed by a higher-level test utility or framework.
No dependency data recorded yet.