How to use the fakeServer function from sinon

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

20
21
22
23
24
25
26
27
28
29
  });
});
describe('receiving a github:gist action', () => {
  let mock = null;
  beforeEach(() => {
    mock = sinon.fakeServer.create();
  });
  afterEach(() => mock.restore());
  it('should send application:sethash for existing gist', (done) => {
    const action = 'github:gist';
fork icon182
star icon749
watch icon35

1
2
3
4
5
6
7
8
9
10
var sinon = require('sinon');
var tosource = require('tosource');
var Widget = require('../../src/Widget.js');

function makeFakeServer() {
  var server = sinon.fakeServer.create();
  server.respondToRun = function respondToRun(body) {
    server.respondWith('POST', '/run/', [
      status,
      { 'Content-Type': 'text/plain' },
fork icon10
star icon153
watch icon10

47
48
49
50
51
52
53
54
55
56
//save xhr
currentXhr = global.window.XMLHttpRequest;
//delete current xhr
delete global.window.XMLHttpRequest;
//create new server
Server = sinon.fakeServer.create();
//copy over fake xhr
window.XMLHttpRequest = Server.xhr;
// get a reference to the original onCreate method 
var onCreate = window.XMLHttpRequest.onCreate; 
fork icon1
star icon2
watch icon2

+ 3 other calls in file

4
5
6
7
8
9
10
11
12
13
var sinon = require('sinon');

var server;

before(function () {
  server = sinon.fakeServer.create();
});

after(function () {
  server.restore();
fork icon6
star icon1
watch icon4

+ 7 other calls in file

185
186
187
188
189
190
191
192
193
194
The preceding example shows how flexible this API is. If it looks too laborous, you may like the fake server:

```javascript
var server;

before(function () { server = sinon.fakeServer.create(); });
after(function () { server.restore(); });

it("calls callback with deserialized data", function () {
    var callback = sinon.fake();
fork icon810
star icon0
watch icon2

+ 3 other calls in file

7
8
9
10
11
12
13
14
15
16
var defaultConfig = {
  autoRespond: true,
  respondImmediately: true
};

var server = sinon.fakeServer.create(_.defaults(config, defaultConfig));

var that = {
  post: method('POST'),
  get: method('GET'),
fork icon5
star icon30
watch icon28

102
103
104
105
106
107
108
109
110
111
}

return storage.load(fixtureCollectionName)
  .then((fixtures) => {
    // Load all of our fixtures into a fake server.
    const fakeServer = sinon.fakeServer.create()
    fakeServer.autoRespond = true
    fakeServer.autoRespondAfter = 0
    fakeServer.respondWith(/.*/, (xhr) => {
      const matchingFixtures = fixtureHelper.findForSinonXHR(fixtures, xhr)
fork icon1
star icon4
watch icon9

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

describe.only("when asyncResult has multiple static, dynamic and promised groups", () => {
  beforeAll(() => {
    const delayFakeServer = sinon.fakeServer.create();

    delayFakeServer.respondWith("GET", /\/group3\.json/, [
      200,
      { "Content-Type": "application/json" },
fork icon0
star icon0
watch icon0

36
37
38
39
40
41
42
43
44
45
    return this._webGLContext;
};

window.useFakeXMLHttpRequest = function() {
    sinon.xhr.supportsCORS = true;
    window.server = sinon.fakeServer.create();
    window.XMLHttpRequest = window.server.xhr;
};

window.URL.revokeObjectURL = function () {};
fork icon0
star icon0
watch icon25

+ 3 other calls in file