How to use the useFakeXMLHttpRequest function from sinon

Find comprehensive JavaScript sinon.useFakeXMLHttpRequest code examples handpicked from public code repositorys.

158
159
160
161
162
163
164
165
166

```javascript
var xhr, requests;

before(function () {
    xhr = sinon.useFakeXMLHttpRequest();
    requests = [];
    xhr.onCreate = function (req) { requests.push(req); };
});
fork icon810
star icon0
watch icon2

+ 3 other calls in file

69
70
71
72
73
74
75
76
77
78
.then((fixtures) => {
  // Want this available in memory.
  storageFixtures = fixtures

  // Sinon's fake XHR logs extra request info we'll need/want for our fixtures:
  sinon.useFakeXMLHttpRequest()

  // Always call through as we're recording and want to make the request:
  window.XMLHttpRequest.useFilters = true
  window.XMLHttpRequest.addFilter(() => true) // true = allow, false = stub
fork icon1
star icon4
watch icon9

205
206
207
208
209
210
211
212
213
214

expect.exportAssertion(
  '<any> with xhr mocked out <object|array> <assertion>',
  function(expect, subject, requestDescriptions) {
    expect.errorMode = 'nested';
    var mockedXhr = sinon.useFakeXMLHttpRequest();

    if (!Array.isArray(requestDescriptions)) {
      requestDescriptions = [requestDescriptions];
    } else {
fork icon1
star icon0
watch icon1

17
18
19
20
21
22
23
24
25
26
const uid = 'odkSxashOmg9QeyRL2cRs00Jke12';

fs.readFile(`${__dirname}/../../app/signin.html`, 'utf8', async (err, data) => {
  describe('#signin functions', () => {
    before( () => {
      global.XMLHttpRequest = sinon.useFakeXMLHttpRequest();
      guidSpy = sinon.spy(module, 'guidVal');
      signInSpy = sinon.spy(module, 'signIn');
    });
    describe('#guidVal', () => {
fork icon1
star icon0
watch icon3

+ 3 other calls in file

6
7
8
9
10
11
12
13
14
15
16


describe('Basic tests', function () {
  var that = this;
  beforeEach(function () {
    that.requests = [];
    that.xhr = sinon.useFakeXMLHttpRequest();
    that.xhr.onCreate = function (xhr) {
      that.requests.push(xhr);
    };
  });
fork icon0
star icon0
watch icon0

49
50
51
52
53
54
55
56
57
58
  });
});


before(function () {
  sinon.useFakeXMLHttpRequest();
});


/*** Kick off mocha, called at the end of test entry files ***/
fork icon0
star icon0
watch icon2