{"library":"nise","title":"Nise Fake XHR and Server","description":"Nise (偽) is a JavaScript library designed for faking XMLHttpRequest (XHR) and creating a fake server environment, primarily used in testing scenarios. It allows developers to intercept and control network requests made by their application without actually performing real HTTP calls. Extracted from the larger Sinon.JS library, Nise offers a lightweight alternative for users who only require XHR and server mocking functionalities, avoiding the overhead of the full Sinon.JS suite. The current stable version is 6.1.5, which was recently published as a patch. The project maintains an active, albeit slower, release cadence for minor and patch versions, driven by community needs and bug fixes. Its key differentiator is its focused scope and tight integration within the Sinon.JS testing ecosystem, providing a reliable and robust solution for simulating server interactions during unit and integration tests.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install nise"],"cli":null},"imports":["import { fakeXhr } from 'nise';","import { fakeServer } from 'nise';","import { FakeXMLHttpRequest } from 'nise';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { fakeXhr, fakeServer } from 'nise';\n\n// 1. Using fakeXhr to replace the global XMLHttpRequest\nlet xhr: typeof XMLHttpRequest | undefined;\nlet capturedRequests: XMLHttpRequest[] = [];\n\nfunction setupFakeXhr() {\n  xhr = fakeXhr.useFakeXMLHttpRequest();\n  xhr.onCreate = function (req) {\n    capturedRequests.push(req);\n  };\n  console.log('Fake XHR initialized.');\n}\n\nfunction teardownFakeXhr() {\n  if (xhr) {\n    xhr.restore();\n    capturedRequests = [];\n    console.log('Fake XHR restored.');\n  }\n}\n\n// 2. Using fakeServer for more complete control and automatic global replacement\nlet server: InstanceType<typeof fakeServer> | undefined;\n\nfunction setupFakeServer() {\n  server = fakeServer.create();\n  // Configure a response for a GET request to /users\n  server.respondWith('GET', '/users', [\n    200, // HTTP Status Code\n    { 'Content-Type': 'application/json' }, // Headers\n    JSON.stringify([{ id: 1, name: 'Alice' }]) // Response Body\n  ]);\n  // Configure a response for a POST request to /users\n  server.respondWith('POST', '/users', [\n    201,\n    { 'Content-Type': 'application/json' },\n    JSON.stringify({ id: 2, name: 'Bob' })\n  ]);\n  console.log('Fake Server initialized.');\n}\n\nfunction teardownFakeServer() {\n  if (server) {\n    server.restore();\n    console.log('Fake Server restored.');\n  }\n}\n\n// --- Demonstration with fakeXhr ---\nsetupFakeXhr();\nconst req1 = new XMLHttpRequest();\nreq1.open('GET', '/data');\nreq1.send();\nconsole.log(`Fake XHR captured request method: ${capturedRequests[0].method}, URL: ${capturedRequests[0].url}`);\nteardownFakeXhr();\n\nconsole.log('\\n--- Demonstration with fakeServer ---\\n');\n\n// --- Demonstration with fakeServer ---\nsetupFakeServer();\n// Make a GET request\nconst req2 = new XMLHttpRequest();\nreq2.open('GET', '/users');\nreq2.onload = function() {\n  console.log(`Fake Server GET response: ${req2.status} - ${req2.responseText}`);\n};\nreq2.send();\nserver!.respond(); // Manually trigger server response\n\n// Make a POST request\nconst req3 = new XMLHttpRequest();\nreq3.open('POST', '/users');\nreq3.onload = function() {\n  console.log(`Fake Server POST response: ${req3.status} - ${req3.responseText}`);\n};\nreq3.setRequestHeader('Content-Type', 'application/json');\nreq3.send(JSON.stringify({ name: 'Bob' }));\nserver!.respond(); // Manually trigger server response\n\nteardownFakeServer();","lang":"typescript","description":"This example demonstrates how to use `fakeXhr` to intercept XMLHttpRequest calls globally and `fakeServer` to define specific responses for different HTTP requests. It shows both setup and teardown for proper test isolation and preventing side effects.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}