Skip to content

Commit

Permalink
fix: Changes self to global, rollup-plugin-node-globals makes isomorp…
Browse files Browse the repository at this point in the history
…hic (#54)

* fix: Changes self to global, rollup-plugin-node-globals makes isomorphic

* chore: Adding linting rule for whitelisting global
  • Loading branch information
jasonmit authored and offirgolan committed Jul 2, 2018
1 parent 7135f67 commit 3811e9d
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ module.exports = {
},
plugins: ['prettier'],
extends: ['eslint:recommended', 'prettier'],
globals: {
global: true
},
env: {
browser: true,
es6: true
Expand Down
10 changes: 5 additions & 5 deletions packages/@pollyjs/core/src/adapters/fetch/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import Adapter from '@pollyjs/adapter';
import serializeHeaders from './utils/serialize-headers';

const nativeFetch = self.fetch;
const nativeFetch = global.fetch;
const { defineProperty } = Object;

export default class FetchAdapter extends Adapter {
onConnect() {
this.assert('Fetch global not found.', nativeFetch);
this.assert(
'Running concurrent fetch adapters is unsupported, stop any running Polly instances.',
self.fetch === nativeFetch
global.fetch === nativeFetch
);

this.native = nativeFetch;
self.fetch = (url, options = {}) =>
global.fetch = (url, options = {}) =>
this.handleRequest({
url,
method: options.method || 'GET',
Expand All @@ -24,7 +24,7 @@ export default class FetchAdapter extends Adapter {
}

onDisconnect() {
self.fetch = nativeFetch;
global.fetch = nativeFetch;
}

async onRecord(pollyRequest) {
Expand All @@ -50,7 +50,7 @@ export default class FetchAdapter extends Adapter {
async onPassthrough(pollyRequest) {
const [, options] = pollyRequest.requestArguments;

const response = await this.native.apply(self, [
const response = await this.native.apply(global, [
pollyRequest.url,
{
...options,
Expand Down
2 changes: 1 addition & 1 deletion packages/@pollyjs/core/src/adapters/xhr/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class XHRAdapter extends Adapter {
this.assert('XHR global not found.', FakeXHR.xhr.supportsXHR);
this.assert(
'Running concurrent XHR adapters is unsupported, stop any running Polly instances.',
self.XMLHttpRequest === FakeXHR.xhr.GlobalXMLHttpRequest
global.XMLHttpRequest === FakeXHR.xhr.GlobalXMLHttpRequest
);

this.native = FakeXHR.xhr.GlobalXMLHttpRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Persister from '@pollyjs/persister';
import stringify from 'json-stable-stringify';

export default class LocalStoragePersister extends Persister {
constructor(polly, store = self.localStorage) {
constructor(polly, store = global.localStorage) {
super(polly);
this._store = store;
this._namespace = '__pollyjs__';
Expand Down
5 changes: 3 additions & 2 deletions packages/@pollyjs/core/src/persisters/rest/ajax.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { keys } = Object;
const REQUEST_ASYNC = !/PhantomJS/.test(self.navigator.userAgent);
const NativeXMLHttpRequest = self.XMLHttpRequest;
const REQUEST_ASYNC =
!('navigator' in global) || !/PhantomJS/.test(global.navigator.userAgent);
const NativeXMLHttpRequest = global.XMLHttpRequest;

export default function ajax(url, options = {}) {
return new Promise((resolve, reject) => {
Expand Down
6 changes: 3 additions & 3 deletions packages/@pollyjs/core/src/test-helpers/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ function generateRecordingName(context) {
return parts.reverse().join('/');
}

export default function setupMocha(defaults = {}, ctx = self) {
export default function setupMocha(defaults = {}, ctx = global) {
setupMocha.beforeEach(defaults, ctx);
setupMocha.afterEach(ctx);
}

setupMocha.beforeEach = function setupMochaBeforeEach(defaults, ctx = self) {
setupMocha.beforeEach = function setupMochaBeforeEach(defaults, ctx = global) {
ctx.beforeEach(function() {
return beforeEach(this, generateRecordingName(this), defaults);
});
};

setupMocha.afterEach = function setupMochaAfterEach(ctx = self) {
setupMocha.afterEach = function setupMochaAfterEach(ctx = global) {
ctx.afterEach(function() {
return afterEach(this, 'mocha');
});
Expand Down
15 changes: 9 additions & 6 deletions packages/@pollyjs/core/tests/helpers/setup-fetch.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* globals beforeEach, afterEach */
import xhrRequest from './xhr-request';

export default function setupFetch(fetchType) {
beforeEach(fetchType);
afterEach();
beforeEachWrapper(fetchType);
afterEachWrapper();
}

export function beforeEach(fetchType) {
self.beforeEach(function() {
function beforeEachWrapper(fetchType) {
beforeEach(function() {
this.fetch = (...args) => {
if (fetchType === 'xhr') {
return xhrRequest(...args);
Expand All @@ -23,10 +24,12 @@ export function beforeEach(fetchType) {
});
}

export function afterEach() {
self.afterEach(async function() {
export function afterEachWrapper() {
afterEach(async function() {
this.polly.stop();

await this.fetchRecord({ method: 'DELETE' });
});
}
export { beforeEachWrapper as beforeEach };
export { afterEachWrapper as afterEach };
6 changes: 3 additions & 3 deletions packages/@pollyjs/core/tests/unit/polly-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Adapter from '@pollyjs/adapter';
import Persister from '@pollyjs/persister';
import { MODES } from '@pollyjs/utils';

const nativeFetch = self.fetch;
const nativeFetch = global.fetch;

describe('Unit | Polly', function() {
it('should exist', function() {
Expand Down Expand Up @@ -201,9 +201,9 @@ describe('Unit | Polly', function() {
});

it('should connect to new adapters', async function() {
expect(nativeFetch).to.equal(self.fetch);
expect(nativeFetch).to.equal(global.fetch);
this.polly.configure({ adapters: ['fetch'] });
expect(nativeFetch).to.not.equal(self.fetch);
expect(nativeFetch).to.not.equal(global.fetch);
});
});

Expand Down

0 comments on commit 3811e9d

Please sign in to comment.