Registry / testing / fake-xml-http-request

fake-xml-http-request

JSON →
library2.1.2jsnpmunverified

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-request
INSTALL
IMPORT
SIG · FAKE-XML-HTTP-REQU
F
fake-xml-http-request
testingjavascriptv2.1.2
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.

FakeXMLHttpRequest
import FakeXMLHttpRequest from 'fake-xml-http-request';
const FakeXMLHttpRequest = require('fake-xml-http-request');
The library primarily uses ESM syntax in its examples, though older Node.js environments might default to CommonJS. It's intended for browser-like environments.
FakeXMLHttpRequest.prototype.respond
xhr.respond(200, {'Content-Type': 'application/json'}, '{"key":"value"}');
xhr.response(200, ...);
The key method for simulating a response is `respond`, not `response`.
FakeXMLHttpRequest lifecycle methods (open, send, abort)
const xhr = new FakeXMLHttpRequest(); xhr.open('GET', '/api/data'); xhr.send(); xhr.abort();
new FakeXMLHttpRequest().open(...).send();
Though chained calls might work for some methods, it's safer to instantiate and then call methods individually, especially when dealing with asynchronous behavior or event listeners.

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.

import FakeXMLHttpRequest from 'fake-xml-http-request'; // Simulate a successful GET request let xhrSuccess = new FakeXMLHttpRequest(); xhrSuccess.open('GET', '/api/users/1'); xhrSuccess.send(); xhrSuccess.respond(200, {'Content-Type': 'application/json'}, '{"id":1, "name":"John Doe"}'); console.log('Success Response:'); console.log('Status:', xhrSuccess.status); console.log('Status Text:', xhrSuccess.statusText); console.log('Response Text:', xhrSuccess.responseText); // Simulate a network error let xhrError = new FakeXMLHttpRequest(); xhrError.open('POST', '/api/data'); xhrError.send(); xhrError.respond(0, {}, null); // Status 0 for network errors console.log('\nError Response:'); console.log('Status:', xhrError.status); console.log('Status Text:', xhrError.statusText); console.log('Response Text:', xhrError.responseText); // Simulate an aborted request let xhrAbort = new FakeXMLHttpRequest(); xhrAbort.open('GET', '/api/long-running-task'); xhrAbort.send(); xhrAbort.abort(); console.log('\nAbort Status:', xhrAbort.status);
Debug
Known issues
gotchaThis library *only* provides a fake XMLHttpRequest object and does not automatically swap the native `XMLHttpRequest` in the global scope. Users must manually manage the instantiation and usage of `FakeXMLHttpRequest` in their tests or integrate it with a testing framework that handles XHR interception.
fix
Manually instantiate `new FakeXMLHttpRequest()` or integrate with a higher-level mocking library (like Pretender.js or Sinon.JS) that handles global XHR replacement.
affects: >=2.0.0
gotchaThe library does not include mechanisms for recording requests, finding specific requests, or playing back recorded interactions. Its functionality is limited to manually responding to individual `FakeXMLHttpRequest` instances.
fix
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.
affects: >=2.0.0
deprecatedThe package has not been updated in over five years (as of April 2026), with the last publish date for version 2.1.2 being March 9, 2021. This means it may not adhere to the absolute latest XMLHttpRequest specification changes or best practices in modern JavaScript testing environments.
fix
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.
affects: >=2.1.2
Errors
Common errors & fixes
ReferenceError: XMLHttpRequest is not defined
Attempting to use `FakeXMLHttpRequest` in a Node.js environment without a global browser-like `XMLHttpRequest` object, or expecting it to automatically polyfill/define `XMLHttpRequest` globally.
fix
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`.
TypeError: Cannot read properties of undefined (reading 'respond')
Calling `respond` or other `FakeXMLHttpRequest` methods on an object that is not an instance of `FakeXMLHttpRequest` or before the XHR object is properly initialized.
fix
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.
My tests are not intercepting any XMLHttpRequest calls.
This library does not automatically intercept global `XMLHttpRequest` calls made by your application code. It only provides a standalone fake `XMLHttpRequest` object.
fix
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.
Upgrade
Version history
2.1.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
5
Resources
fake-xml-http-request — npm install fake-xml-http-request · libregistry