Skip to content

Instantly share code, notes, and snippets.

@dominics
Last active November 23, 2015 04:40
Show Gist options
  • Save dominics/423f7048ecb60d9db79b to your computer and use it in GitHub Desktop.
Save dominics/423f7048ecb60d9db79b to your computer and use it in GitHub Desktop.
Mocha + Sinon FakeServer unit test, no browser required, runs entirely in node
import sinon from 'sinon';
import jsdom from 'jsdom';
global.document = jsdom.jsdom('<!doctype html><html><body></body></html>');
global.window = document.defaultView;
global.sinon = sinon;
// because otherwise sinon will try to use window.XDomainRequest and fail
sinon.xhr.supportsCORS = true;
// because sinon refuses to look at window.XMLHttpRequest, and insists it's in global scope
global.XMLHttpRequest = global.window.XMLHttpRequest;
// [...]
describe('client api class Setting', function() {
before(() => {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.respondWith('/api/setting/all', [
200,
{ 'Content-Type': 'application/json' },
'{"type": "setting", "data": [{ "id": "foo", "value": true }]}',
]);
this.setting = new Setting();
});
describe('.refresh()', () => {
it('eventually returns an array of settings', () => {
return expect(this.setting.refresh()).to.eventually.be.instanceOf(Array);
});
});
after(() => {
this.server.restore();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment