Skip to content

Commit

Permalink
fix(analytics-client-common): adding ampIntegrationsContext to getGlo…
Browse files Browse the repository at this point in the history
…balScope (#783)
  • Loading branch information
zacbennett authored Jun 24, 2024
2 parents 79b78f3 + 4176f5f commit 84e78e6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/analytics-client-common/src/global-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
/* Only file allowed to access to globalThis, window, self */

export const getGlobalScope = (): typeof globalThis | undefined => {
// This should only be used for integrations with Amplitude that are not running in a browser environment
// We need to specify the name of the global variable as a string to prevent it from being minified
const ampIntegrationContextName = 'ampIntegrationContext' as keyof typeof globalThis;
if (typeof globalThis !== 'undefined' && typeof globalThis[ampIntegrationContextName] !== 'undefined') {
return globalThis[ampIntegrationContextName] as typeof globalThis;
}
if (typeof globalThis !== 'undefined') {
return globalThis;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/analytics-client-common/src/types/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-disable no-restricted-globals */
// eslint-disable-next-line no-var
export var ampIntegrationContext: typeof globalThis;
45 changes: 45 additions & 0 deletions packages/analytics-client-common/test/global-scope.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { getGlobalScope } from '../src/global-scope';

describe('getGlobalScope', () => {
let originalGlobalThis: any;

beforeEach(() => {
originalGlobalThis = globalThis;
});

afterEach(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
globalThis = originalGlobalThis;
delete (globalThis as any).ampIntegrationContext;
});

test('returns ampIntegrationContext if it exists', () => {
(globalThis as any).ampIntegrationContext = { someKey: 'someValue' };
expect(getGlobalScope()).toBe((globalThis as any).ampIntegrationContext);
});

test('returns globalThis if ampIntegrationContext does not exist', () => {
const scope = getGlobalScope();
// Need to use Object.is because expect(scope).toBe(globalThis) will throw an error
expect(Object.is(scope, globalThis)).toBeTruthy();
});

test('should return window if globalThis is undefined and window is defined', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
globalThis = undefined;

const scope = getGlobalScope();

// Note: We NEED to reassign globalThis to its original state because the jest expect function requires it
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
globalThis = originalGlobalThis;
// Need to use Object.is because expect(scope).toBe(globalThis) will throw an error
expect(Object.is(scope, window)).toBeTruthy();
});
});

0 comments on commit 84e78e6

Please sign in to comment.