unmock-xmlhttprequest
v0.3.18
Published
[![npm](https://img.shields.io/npm/v/unmock-xmlhttprequest.svg)](https://www.npmjs.com/package/unmock-xmlhttprequest) [![CircleCI](https://circleci.com/gh/unmock/unmock-js.svg?style=svg)](https://circleci.com/gh/unmock/unmock-js) [![codecov](https://codec
Downloads
473
Readme
unmock-xmlhttprequest
Interceptor and faker for xmlhttprequest.
Installation
npm i -D unmock-xmlhttprequest
yarn add unmock-xmlhttprequest -D
Usage
Usage as monkey-patched XMLHhttpRequest
import { replaceOpenAndReturnOriginal } from "unmock-xmlhttprequest";
import { ISerializedRequest, ISerializedResponse, OnSerializedRequest } from "unmock-core";
// Define what to do with the intercepted request
const responseCreator = (
req: ISerializedRequest
): ISerializedResponse => {
return {
headers: {},
statusCode: 200,
body: JSON.stringify({
ok: true,
}),
};
};
// Build `fetch` with your callback
const originalOpen = replaceOpenAndReturnOriginal(responseCreator);
// Make API calls!
const request = new XMLHttpRequest();
request.open("GET", "https://example.com");
request.onload = () => {
const asJson = JSON.parse(request.responseText);
expect(asJson.ok).toBe(true);
}
request.send();