xhr-mock
Utility for mocking XMLHttpRequest
.
Great for testing. Great for prototyping while your backend is still being built.
Works in NodeJS and in the browser. Is compatible with Axios, jQuery, Superagent
and probably every other library built on XMLHttpRequest
. Standard compliant (http://xhr.spec.whatwg.org/).
Documentation
Installation
Using a bundler
If you are using a bundler like Webpack or Browserify then install xhr-mock
using yarn
or npm
:
yarn add --dev xhr-mock
Now import xhr-mock
and start using it in your scripts:
;
Without a bundler
If you aren't using a bundler like Webpack or Browserify then add this script to your HTML:
Now you can start using the global, XHRMock
, in your scripts.
Usage
First off lets write some code that uses XMLHttpRequest
...
./createUser.js
// we could have just as easily use Axios, jQuery, Superagent// or another package here instead of using the native XMLHttpRequest object { return { const xhr = ; xhr { if xhrreadyState == XMLHttpRequestDONE if xhrstatus === 201 try ; catch error ; else if xhrstatus try ; catch error ; else ; }; xhr; xhr; xhr; };}
Now lets test the code we've written...
./createUser.test.js
;; ;
API
xhr-mock
.setup()
Replace the global XMLHttpRequest
object with the MockXMLHttpRequest
.
.teardown()
Restore the global XMLHttpRequest
object to its original state.
.reset()
Forget all the request handlers.
.get(url | regex, mock)
Register a factory function to create mock responses for each GET request to a specific URL.
mock;
.post(url | regex, mock)
Register a factory function to create mock responses for each POST request to a specific URL.
.put(url | regex, mock)
Register a factory function to create mock responses for each PUT request to a specific URL.
.patch(url | regex, mock)
Register a factory function to create mock responses for each PATCH request to a specific URL.
.delete(url | regex, mock)
Register a factory function to create mock responses for each DELETE request to a specific URL.
.use(method, url | regex, mock)
Register a factory function to create mock responses for each request to a specific URL.
.use(fn)
Register a factory function to create mock responses for every request.
.error(fn)
Log errors thrown by handlers.
MockXMLHttpRequest
MockRequest
.method() : string
Get the request method.
.url() : MockURL
Get the request URL.
.header(name : string, value: string)
Set a request header.
.header(name : string) : string | null
Get a request header.
.headers() : object
Get the request headers.
.headers(headers : object)
Set the request headers.
.body() : string
Get the request body.
.body(body : string)
Set the request body.
MockResponse
.status() : number
Get the response status.
.status(code : number)
Set the response status.
.reason() : string
Get the response reason.
.reason(phrase : string)
Set the response reason.
.header(name : string, value: string)
Set a response header.
.header(name : string) : string | null
Get a response header.
.headers() : object
Get the response headers.
.headers(headers : object)
Set the response headers.
.body() : string
Get the response body.
.body(body : string)
Set the response body.
How to?
Simulate progress
Upload progress
Set the Content-Length
header and send a body. xhr-mock
will emit ProgressEvent
s.
; mock; mock; const xhr = ;xhrupload console;xhr;xhr;xhr;
Download progress
Set the Content-Length
header and send a body. xhr-mock
will emit ProgressEvent
s.
; mock; mock; const xhr = ;xhr console;xhr;xhr;
Simulate a timeout
Return a Promise
that never resolves or rejects.
; mock; mock; const xhr = ;xhrtimeout = 100;xhr console;xhr;xhr;
A number of major libraries don't use the
timeout
event and usesetTimeout()
instead. Therefore, in order to mock timeouts in major libraries, we have to wait for the specified amount of time anyway.
Simulate an error
Return a Promise
that rejects. If you want to test a particular error you an use one of the pre-defined error classes.
; mock; mock; const xhr = ;xhr console;xhr;xhr;
Proxying requests
If you want to mock some requests, but not all of them, you can proxy unhandled requests to a real server.
; mock; // mock specific requestsmock; // proxy unhandled requests to the real serversmock; // this request will receive a mocked responseconst xhr1 = ;xhr1;xhr1; // this request will receieve the real responseconst xhr2 = ;xhr2;xhr2;
Delaying requests
Requests can be delayed using our handy delay
utility.
; mock; // delay the request for three secondsmock;
Once off requests
Requests can be made on one off occasions using our handy once
utility.
; mock; // the response will only be returned the first time a request is mademock;
send a sequence of responses
In case you need to return a different response each time a request is made, you may use the sequence
utility.
; mock; mock;
License
MIT Licensed. Copyright (c) James Newell 2014.