Skip to content

Commit

Permalink
Merge pull request #47 from jhnns/fix/illegal-invocation
Browse files Browse the repository at this point in the history
Fix illegal invocation errors
  • Loading branch information
developit authored Jun 28, 2017
2 parents 2ec508a + e066eb8 commit 04c6d24
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default typeof fetch=='function' ? fetch : function(url, options) {
export default typeof fetch=='function' ? fetch.bind() : function(url, options) {
options = options || {};
return new Promise( (resolve, reject) => {
let request = new XMLHttpRequest();
Expand Down
19 changes: 18 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,31 @@ import fetch from '../src';
import chai, { expect } from 'chai';
import { spy, stub } from 'sinon';
import sinonChai from 'sinon-chai';
chai.use(sinonChai);
import { transformFileSync } from 'babel-core';
import vm from 'vm';

chai.use(sinonChai);

describe('unfetch', () => {
it('should be a function', () => {
expect(fetch).to.be.a('function');
});

// Prevents illegal invocation errors
// See https://github.com/developit/unfetch/issues/46
it('should bind the native fetch on window', () => {
const filename = require.resolve('../src');
const sandbox = {
window: {},
fetch() { return this },
exports: {}
};

vm.runInNewContext(transformFileSync(filename).code, sandbox, filename);

expect(sandbox.exports.default()).to.equal(sandbox.window);
});

describe('fetch()', () => {
it('sanity test', () => {
let xhr = {
Expand Down

0 comments on commit 04c6d24

Please sign in to comment.