{"id":14385156,"url":"https://github.com/algolia/faux-jax","last_synced_at":"2026-01-10T12:55:04.372Z","repository":{"id":57233513,"uuid":"30595879","full_name":"algolia/faux-jax","owner":"algolia","description":"NO MORE MAINTAINED: Intercept and respond to requests in the browser (AJAX) and Node.js (http(s) module)","archived":true,"fork":false,"pushed_at":"2019-02-28T08:45:09.000Z","size":139,"stargazers_count":95,"open_issues_count":7,"forks_count":9,"subscribers_count":80,"default_branch":"master","last_synced_at":"2024-12-15T16:15:03.633Z","etag":null,"topics":["ajax","http","mock","test","xdomainrequest","xmlhttprequest"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/algolia.png","metadata":{"files":{"readme":"readme.md","changelog":"HISTORY.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-02-10T14:29:56.000Z","updated_at":"2023-11-07T12:39:06.000Z","dependencies_parsed_at":"2022-08-31T14:11:37.757Z","dependency_job_id":null,"html_url":"https://github.com/algolia/faux-jax","commit_stats":null,"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/algolia%2Ffaux-jax","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/algolia%2Ffaux-jax/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/algolia%2Ffaux-jax/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/algolia%2Ffaux-jax/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/algolia","download_url":"https://codeload.github.com/algolia/faux-jax/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230720987,"owners_count":18270485,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ajax","http","mock","test","xdomainrequest","xmlhttprequest"],"created_at":"2024-08-28T18:01:59.678Z","updated_at":"2026-01-10T12:55:04.329Z","avatar_url":"https://github.com/algolia.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"**MESSAGE FROM MAINTAINERS**: This module is not maintained, you can fork it still or there are maybe better solutions nowadays.\n\n# faux-jax [![Version Badge][npm-version-svg]][package-url] [![Build Status][travis-svg]][travis-url] [![License][license-image]][license-url] [![Downloads][downloads-image]][downloads-url]\n\n[![Browser tests][browser-test-matrix]][browser-test-url]\n\nIntercept and respond to:\n  - [XMLHttpRequest](https://xhr.spec.whatwg.org/)\n  - [XDomainRequest](https://msdn.microsoft.com/en-us/library/ie/cc288060(v=vs.85).aspx) in [compatible environments](#how)\n  - Node.js [http(s)](https://nodejs.org/api/http.html) module\n\n```sh\nnpm install faux-jax --save[-dev]\n```\n\n# Browser example\n\n```js\nvar fauxJax = require('faux-jax');\n\nfauxJax.install();\n\ndoRequest();\nfauxJax.on('request', respond);\n\n// somewhere in your code:\nfunction doRequest() {\n  var xhr = new XMLHttpRequest();\n\n  xhr.open('POST', '/dawg');\n  xhr.setRequestHeader('Content-Type', 'application/json');\n  xhr.send(\n    JSON.stringify({\n      YAW: 'dawg'\n    })\n  );\n  xhr.onload = function() {\n    console.log(xhr.status); // 200\n    console.log(xhr.response); // {zup: 'bro'}\n  }\n}\n\n// in a test file probably:\nfunction respond(request) {\n  request.respond(\n    200, { // status\n      'Content-Type': 'application/json' // headers\n    },\n    '{\"zup\": \"bro?\"}' //body\n  );\n\n  fauxJax.restore();\n}\n```\n\n# Node.js example\n\n```js\nvar http = require('http');\nvar fauxJax = require('faux-jax');\n\nfauxJax.install();\n\ndoRequest();\nfauxJax.on('request', respond);\n\nfunction doRequest() {\n  http.request('http://www.google.com', function(res) {\n    console.log(res.statusCode); // 200\n\n    var chunks = [];\n    res.on('data', function(chunk) {\n      chunks.push(chunk);\n    });\n\n    res.on('end', function() {\n      console.log(Buffer.concat(chunks).toString());\n    });\n  }).end();\n}\n\nfunction respond(request) {\n  request.respond(\n    200, { // status\n      'Content-Type': 'text/plain' // headers\n    },\n    'Hello Node.js!' //body\n  );\n\n  fauxJax.restore();\n}\n```\n\n# API\n\n## fauxJax.install([opts])\n\nReplace global `XMLHttpRequest` and `XDomainRequest` with mocks.\n\n* `opts.gzip`: boolean. Set to true in nodejs to receive gzipped responses.\n\n## fauxJax.on('request', cb)\n\nfauxJax is an [EventEmitter](https://nodejs.org/api/events.html#events_class_events_eventemitter).\n\nEverytime a new request is made, you will get a `request` event.\n\nYou can listen to it with `cb(request)`.\n\nAll requests have the native properties/methods from [the spec](https://xhr.spec.whatwg.org/).\n\nWe also added a couple of handy properties/methods for you to ease testing.\n\n## fauxJax.waitFor(nbRequests, cb)\n\nUtility to \"wait for n requests\". Will call `cb(err, requests)`.\n\n### request.requestMethod\n\n### request.requestURL\n\n### request.requestHeaders\n\nAlways `{}` with `XDomainRequest`.\n\n### request.requestBody\n\n### request.respond(status[, headers, body])\n\n### request.setResponseHeaders(headers)\n\n### request.setResponseBody(body[, cb])\n\n## fauxJax.restore()\n\nSets back global `XMLHttpRequest` and `XDomainRequest` to native implementations.\n\n## fauxJax.support\n\nObject containing [various support flags](./lib/support.js) for your tests, used internally by `faux-jax`.\n\n## Errors\n\nErrors will be emitted when:\n  - you try to `.install()` when already installed\n  - you try to `.restore()` without calling `.install()`\n  - **a request was intercepted while no listener set**\n\n# How\n\ntl;dr; We try to be as close as possible to the mocked native environment.\n\n`faux-jax` uses [feature detection](./lib/support.js) to only expose what's relevant for the current environment.\n\ni.e. on Chrome, we do not intercept nor expose `XDomainRequest`.\n\nAlso if the browser only implement [some parts](https://dvcs.w3.org/hg/xhr/raw-file/default/xhr-1/Overview.html) of `XMLHttpRequest`, we mimic it.\n\n# Test\n\n```sh\nnpm test\n```\n\n# Develop\n\n```sh\nnpm run dev\n```\n\nGo to \u003chttp://localhost:8080/__zuul\u003e.\n\n[Tests](./test/) are written with [tape](https://github.com/substack/tape) and run through [zuul](https://github.com/defunctzombie/zuul).\n\n# Lint\n\n```sh\nnpm run lint\n```\n\nUses [eslint](http://eslint.org/), see [.eslintrc](./.eslintrc).\n\n# Thanks\n\nInspiration for this module came from:\n- Sinon.js's [Fake XMLHttpRequest](http://sinonjs.org/docs/#server)\n- trek's [FakeXMLHttpRequest](https://github.com/trek/FakeXMLHttpRequest)\n\nMany thanks!\n\nNode.js version is using [moll/node-mitm](https://github.com/moll/node-mitm).\n\n[package-url]: https://npmjs.org/package/faux-jax\n[npm-version-svg]: https://img.shields.io/npm/v/faux-jax.svg?style=flat-square\n[travis-svg]: https://img.shields.io/travis/algolia/faux-jax/master.svg?style=flat-square\n[travis-url]: https://travis-ci.org/algolia/faux-jax\n[license-image]: http://img.shields.io/npm/l/faux-jax.svg?style=flat-square\n[license-url]: LICENSE\n[downloads-image]: https://img.shields.io/npm/dm/faux-jax.svg?style=flat-square\n[downloads-url]: http://npm-stat.com/charts.html?package=faux-jax\n[browser-test-matrix]: https://saucelabs.com/browser-matrix/os-algolia-faux-jax.svg\n[browser-test-url]: https://saucelabs.com/u/os-algolia-faux-jax\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falgolia%2Ffaux-jax","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falgolia%2Ffaux-jax","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falgolia%2Ffaux-jax/lists"}