{"id":13172,"library":"fake-xml-http-request","title":"FakeXMLHttpRequest for Browser Testing","description":"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.","status":"maintenance","version":"2.1.2","language":"javascript","source_language":"en","source_url":"https://github.com/trek/FakeXMLHttpRequest","tags":["javascript"],"install":[{"cmd":"npm install fake-xml-http-request","lang":"bash","label":"npm"},{"cmd":"yarn add fake-xml-http-request","lang":"bash","label":"yarn"},{"cmd":"pnpm add fake-xml-http-request","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"const FakeXMLHttpRequest = require('fake-xml-http-request');","symbol":"FakeXMLHttpRequest","correct":"import FakeXMLHttpRequest from 'fake-xml-http-request';"},{"note":"The key method for simulating a response is `respond`, not `response`.","wrong":"xhr.response(200, ...);","symbol":"FakeXMLHttpRequest.prototype.respond","correct":"xhr.respond(200, {'Content-Type': 'application/json'}, '{\"key\":\"value\"}');"},{"note":"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.","wrong":"new FakeXMLHttpRequest().open(...).send();","symbol":"FakeXMLHttpRequest lifecycle methods (open, send, abort)","correct":"const xhr = new FakeXMLHttpRequest(); xhr.open('GET', '/api/data'); xhr.send(); xhr.abort();"}],"quickstart":{"code":"import FakeXMLHttpRequest from 'fake-xml-http-request';\n\n// Simulate a successful GET request\nlet xhrSuccess = new FakeXMLHttpRequest();\nxhrSuccess.open('GET', '/api/users/1');\nxhrSuccess.send();\nxhrSuccess.respond(200, {'Content-Type': 'application/json'}, '{\"id\":1, \"name\":\"John Doe\"}');\n\nconsole.log('Success Response:');\nconsole.log('Status:', xhrSuccess.status);\nconsole.log('Status Text:', xhrSuccess.statusText);\nconsole.log('Response Text:', xhrSuccess.responseText);\n\n// Simulate a network error\nlet xhrError = new FakeXMLHttpRequest();\nxhrError.open('POST', '/api/data');\nxhrError.send();\nxhrError.respond(0, {}, null); // Status 0 for network errors\n\nconsole.log('\\nError Response:');\nconsole.log('Status:', xhrError.status);\nconsole.log('Status Text:', xhrError.statusText);\nconsole.log('Response Text:', xhrError.responseText);\n\n// Simulate an aborted request\nlet xhrAbort = new FakeXMLHttpRequest();\nxhrAbort.open('GET', '/api/long-running-task');\nxhrAbort.send();\nxhrAbort.abort();\n\nconsole.log('\\nAbort Status:', xhrAbort.status);","lang":"javascript","description":"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."},"warnings":[{"fix":"Manually instantiate `new FakeXMLHttpRequest()` or integrate with a higher-level mocking library (like Pretender.js or Sinon.JS) that handles global XHR replacement.","message":"This 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.","severity":"gotcha","affected_versions":">=2.0.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":">=2.0.0"},{"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.","message":"The 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.","severity":"deprecated","affected_versions":">=2.1.2"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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`.","cause":"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.","error":"ReferenceError: XMLHttpRequest is not defined"},{"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.","cause":"Calling `respond` or other `FakeXMLHttpRequest` methods on an object that is not an instance of `FakeXMLHttpRequest` or before the XHR object is properly initialized.","error":"TypeError: Cannot read properties of undefined (reading 'respond')"},{"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.","cause":"This library does not automatically intercept global `XMLHttpRequest` calls made by your application code. It only provides a standalone fake `XMLHttpRequest` object.","error":"My tests are not intercepting any XMLHttpRequest calls."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}