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(plugin-document-load): check if getEntriesByType is available before using it #259

Merged
merged 3 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ export interface GraphQLInstrumentationConfig {
/**
* Merged and parsed config of default instrumentation config and GraphQL
*/
export type GraphQLInstrumentationParsedConfig = Required<
GraphQLInstrumentationConfig
> &
export type GraphQLInstrumentationParsedConfig = Required<GraphQLInstrumentationConfig> &
InstrumentationConfig;

export type executeFunctionWithObj = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class DocumentLoad extends BasePlugin<unknown> {
* @param rootSpan
*/
private _addResourcesSpans(rootSpan: Span): void {
const resources: PerformanceResourceTiming[] = otperformance.getEntriesByType(
const resources: PerformanceResourceTiming[] = otperformance.getEntriesByType?.(
'resource'
) as PerformanceResourceTiming[];
if (resources) {
Expand Down Expand Up @@ -163,7 +163,7 @@ export class DocumentLoad extends BasePlugin<unknown> {
*/
private _getEntries() {
const entries: PerformanceEntries = {};
const performanceNavigationTiming = (otperformance.getEntriesByType(
const performanceNavigationTiming = (otperformance.getEntriesByType?.(
'navigation'
)[0] as unknown) as PerformanceEntries;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,21 +511,7 @@ describe('DocumentLoad Plugin', () => {
});
});

describe('when navigation entries types are NOT available then fallback to "performance.timing"', () => {
let spyEntries: any;
beforeEach(() => {
spyEntries = sinon.stub(window.performance, 'getEntriesByType');
spyEntries.withArgs('navigation').returns([]);
spyEntries.withArgs('resource').returns([]);
Object.defineProperty(window.performance, 'timing', {
writable: true,
value: entriesFallback,
});
});
afterEach(() => {
spyEntries.restore();
});

function shouldExportCorrectSpan() {
it('should export correct span with events', done => {
const spyOnEnd = sinon.spy(dummyExporter, 'export');
plugin.enable(moduleExports, provider, logger, config);
Expand Down Expand Up @@ -557,6 +543,41 @@ describe('DocumentLoad Plugin', () => {
done();
});
});
}

describe('when navigation entries types are NOT available then fallback to "performance.timing"', () => {
let spyEntries: any;
beforeEach(() => {
spyEntries = sinon.stub(window.performance, 'getEntriesByType');
spyEntries.withArgs('navigation').returns([]);
spyEntries.withArgs('resource').returns([]);

Object.defineProperty(window.performance, 'timing', {
writable: true,
value: entriesFallback,
});
});
afterEach(() => {
spyEntries.restore();
});

shouldExportCorrectSpan();
});

describe('when getEntriesByType is not defined then fallback to "performance.timing"', () => {
beforeEach(() => {
Object.defineProperty(window.performance, 'getEntriesByType', {
writable: true,
value: undefined,
});

Object.defineProperty(window.performance, 'timing', {
writable: true,
value: entriesFallback,
});
});

shouldExportCorrectSpan();
});

describe('when navigation entries types and "performance.timing" are NOT available', () => {
Expand Down