Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(browser): Check for existence of instrumentation targets #8939

Merged
merged 4 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/utils/src/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ export function parseFetchArgs(fetchArgs: unknown[]): { method: string; url: str
}

/** JSDoc */
function instrumentXHR(): void {
if (!('XMLHttpRequest' in WINDOW)) {
export function instrumentXHR(): void {
if (!(WINDOW as any).XMLHttpRequest) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: Why do we need the typecast to any here? Or IOW why do we need it here and not in WINDOW.document below?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignore this, just learned that it's not in WINDOW. Makes sense!

return;
}

Expand Down Expand Up @@ -539,8 +539,8 @@ type InstrumentedElement = Element & {
};

/** JSDoc */
function instrumentDOM(): void {
if (!('document' in WINDOW)) {
export function instrumentDOM(): void {
if (!WINDOW.document) {
return;
}

Expand Down
18 changes: 17 additions & 1 deletion packages/utils/test/instrument.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import { parseFetchArgs } from '../src/instrument';
import { instrumentDOM, instrumentXHR, parseFetchArgs } from '../src/instrument';

jest.mock('../src/worldwide', () => ({
// Return an empty object with undefined properties
getGlobalObject: () => ({
document: undefined,
XMLHttpRequest: undefined,
}),
}));

describe('instrument', () => {
it('instrumentXHR() does not throw if XMLHttpRequest is a key on window but not defined', () => {
expect(instrumentXHR).not.toThrow();
});

it('instrumentDOM() does not throw if XMLHttpRequest is a key on window but not defined', () => {
expect(instrumentDOM).not.toThrow();
});

describe('parseFetchArgs', () => {
it.each([
['string URL only', ['http://example.com'], { method: 'GET', url: 'http://example.com' }],
Expand Down