Skip to content

Commit

Permalink
fix(instrumentation-user-interaction): addEventListener throws when c…
Browse files Browse the repository at this point in the history
…alling with useCapture = null (open-telemetry#1045)

* fix(instrumentation-user-interaction): addEventListener throws when calling with useCapture = null

* lint fix

Co-authored-by: Rauno Viskus <Rauno56@users.noreply.github.com>
Co-authored-by: Daniel Dyla <dyladan@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 20, 2022
1 parent 69efe64 commit 893a9fc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ export class UserInteractionInstrumentation extends InstrumentationBase<unknown>
return original.call(this, type, listener, useCapture);
}

const once = typeof useCapture === 'object' && useCapture.once;
// filter out null (typeof null === 'object')
const once =
useCapture && typeof useCapture === 'object' && useCapture.once;
const patchedListener = function (this: HTMLElement, ...args: any[]) {
let parentSpan: api.Span | undefined;
const event: Event | undefined = args[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,15 @@ describe('UserInteractionInstrumentation', () => {
document.removeEventListener('click', null);
});

it('should handle null useCapture', () => {
const listener = () => {};
// @ts-expect-error Typescript typings report null useCapture as error
// which follows the spec but that doesn't stop users
document.addEventListener('click', listener, null);
// @ts-expect-error see above
document.removeEventListener('click', listener, null);
});

it('should handle disable', () => {
assert.strictEqual(
isWrapped(HTMLElement.prototype.addEventListener),
Expand Down

0 comments on commit 893a9fc

Please sign in to comment.