Fake XHR and server for testing JavaScript applications
npx @tessl/cli install tessl/npm-nise@6.1.0nise provides comprehensive fake XMLHttpRequest (XHR) and server functionality for testing JavaScript applications. It enables developers to intercept, mock, and simulate HTTP requests and responses without making actual network calls, which is essential for unit testing, integration testing, and development environments. As part of the SinonJS ecosystem, nise is designed for maximum compatibility with testing frameworks and provides fine-grained control over network behavior.
npm install niseconst { fakeServer, fakeServerWithClock, fakeXhr } = require("nise");For ES modules (bundled version):
import nise from "nise";
const { fakeServer, fakeServerWithClock, fakeXhr } = nise;const nise = require("nise");
// Create a fake server
const server = nise.fakeServer.create();
// Set up a response
server.respondWith("GET", "/api/users", [
200,
{ "Content-Type": "application/json" },
JSON.stringify([{ id: 1, name: "John" }])
]);
// Enable auto-response
server.autoRespond = true;
// Make a request (in your application code)
// This will be intercepted by the fake server
// Clean up
server.restore();nise is built around three core components:
The library supports both Node.js and browser environments, with feature detection for platform-specific capabilities like timeout handling, CORS support, and different response types (ArrayBuffer, Blob, JSON, XML).
High-level interface for intercepting and responding to HTTP requests with pattern matching, automatic responses, and request history tracking.
const fakeServer = {
/**
* Create a new fake server instance
* @param {Object} [config] - Optional configuration object
* @returns {Object} FakeServer instance
*/
create: function(config) {}
};
// FakeServer instance methods
const server = fakeServer.create();
/**
* Configure response for matching requests
* @param {string} method - HTTP method
* @param {string|RegExp|Function} url - URL pattern
* @param {string|Array|Function} body - Response body
*/
server.respondWith = function(method, url, body) {};
/**
* Process all pending requests
*/
server.respond = function() {};
/**
* Process all requests immediately
*/
server.respondAll = function() {};
/**
* Restore original XMLHttpRequest
*/
server.restore = function() {};
/**
* Reset server state
*/
server.reset = function() {};
/**
* Clear configured responses
*/
server.resetBehavior = function() {};
/**
* Clear request history
*/
server.resetHistory = function() {};
/**
* Get request by index
* @param {number} index - Zero-based request index
* @returns {XMLHttpRequest|null} Request object or null
*/
server.getRequest = function(index) {};Enhanced fake server that integrates with fake timers for testing time-dependent scenarios like request timeouts and delayed responses.
const fakeServerWithClock = {
/**
* Create a fake server with integrated timer support
* @param {Object} [config] - Optional configuration object
* @returns {Object} FakeServerWithClock instance (includes all FakeServer methods)
*/
create: function(config) {}
};Timer-Integrated Server Mocking
Low-level XMLHttpRequest replacement providing complete control over the request/response lifecycle, response types, and progress events.
const fakeXhr = {
/**
* Replace global XMLHttpRequest with fake implementation
* @returns {Function} FakeXMLHttpRequest constructor
*/
useFakeXMLHttpRequest: function() {},
/**
* Create fake XMLHttpRequest for specific global scope
* @param {Object} globalScope - Global object (window/global)
* @returns {Object} Object with xhr utilities and FakeXMLHttpRequest
*/
fakeXMLHttpRequestFor: function(globalScope) {},
/**
* FakeXMLHttpRequest constructor
* @param {Object} [config] - Optional configuration
* @constructor
*/
FakeXMLHttpRequest: function(config) {}
};
// FakeXMLHttpRequest instance methods
const xhr = new fakeXhr.FakeXMLHttpRequest();
/**
* Initialize a request
* @param {string} method - HTTP method
* @param {string} url - Request URL
* @param {boolean} [async=true] - Async flag
* @param {string} [username] - Username for auth
* @param {string} [password] - Password for auth
*/
xhr.open = function(method, url, async, username, password) {};
/**
* Send the request
* @param {string|FormData|ArrayBuffer} [data] - Request body data
*/
xhr.send = function(data) {};
/**
* Set complete response (status, headers, and body)
* @param {number} status - HTTP status code
* @param {Object} headers - Response headers
* @param {string|ArrayBuffer} body - Response body
*/
xhr.respond = function(status, headers, body) {};/**
* Server configuration object
* @typedef {Object} ServerConfig
* @property {boolean} [autoRespond] - Automatically respond to requests after a delay
* @property {number} [autoRespondAfter] - Delay in milliseconds for auto-response
* @property {boolean} [respondImmediately] - Respond to requests immediately when received
* @property {boolean} [fakeHTTPMethods] - Enable HTTP method override via _method parameter
* @property {Function} [logger] - Custom logging function
* @property {boolean} [unsafeHeadersEnabled] - Allow setting unsafe headers
*/
/**
* FakeXMLHttpRequest configuration object
* @typedef {Object} FakeXHRConfig
* @property {Function} [logger] - Custom logging function
* @property {boolean} [useImmediateExceptions] - Throw errors immediately vs on next tick
* @property {Function} [setTimeout] - Custom setTimeout function
*/
/**
* Response configuration object
* @typedef {Object} RequestResponse
* @property {string} [method] - HTTP method to match
* @property {string|RegExp|Function} [url] - URL pattern to match
* @property {string|Array|Function} response - Response data (status, headers, body)
*/