question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReferenceError: PointerEvent is not defined

See original GitHub issue

Issue :

After upgrading to Jest 24, I get this Typescript error:

ReferenceError: PointerEvent is not defined

I use a tsconfig.spec.json (which seems to be loaded by ts-jest) which contains "lib": ["es2017", "dom"] in compilerOptions.

Expected behavior :

I expect PointerEvent to be defined, as it is part of the TypeScript dom lib.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
wthocommented, Apr 24, 2019

I realized, this is not in jest’s hands. Jest uses JSDOM to run the tests, and after a quick check I saw there is already an issue opened in JSDOM to implement PointerEvent.

Basically the Constructor is missing: window.PointerEvent.

I suggest everyone interested in this to subscribe to this issue or even provide a PR to JSDOM: https://github.com/jsdom/jsdom/issues/2527

Once this is implemented in jsdom, it might take some additional time for jest to provide the test environment.

Until then this patching can be used:

// setupEvents.js
window.PointerEvent = function(type, eventInitDict) {}
window.BeforeUnloadEvent = function() {}

It has to be registered in jest.config.js

module.exports = {
  setupFilesAfterEnv: ['./setupEvents']
}

@ahnpnl I think this issue can be closed, there is nothing ts-jest can do about it.

4reactions
wthocommented, Apr 6, 2019

Summarizing the discussion in https://github.com/thymikee/jest-preset-angular/issues/245:

  • Combination of Method Decorator and PointerEvent in the method signature will throw this error
  • Angular is not required to trigger the error
  • tsconfig.spec.json is configured with "types": ["dom"]
  • Other DOM-types like other events do not throw an error
  • any Decorator could be used
  • IDE (VSCode with TS) does not throw error

Minimal reproduction:

export function Decorator() {
  return (...args) => {};
}

class Test {
  @Decorator()
  decoratedMethodWithPointerEventType(event: PointerEvent) { }
}

describe('DOM PointerEvent and Decorators', () => {
  it('should not throw when combining a decorator with the DOM type PointerEvent', () => {
    const t = new Test()
    expect(t).toBeTruthy()
  });
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - How to substitute the PointerEvent in Jest ...
If I test this setup with jest it always throws me the error ReferenceError: PointerEvent is not defined . I can fix this...
Read more >
Solution for “Error: The argument type 'PointerEvent' can't be ...
Solution for “Error: The argument type 'PointerEvent' can't be assigned to the ... Solution for “ReferenceError: util$4 is not defined” for Svelte apps....
Read more >
PointerEvent - Web APIs | MDN
Chrome Edge PointerEvent Full support. Chrome55. Toggle history Full support. Edge12. T... PointerEvent() constructor Full support. Chrome55. Toggle history Full support. Edge12. T... altitudeAngle. Experimental Full...
Read more >
ReferenceError: PointerEvent is not defined - - Bountysource
ReferenceError : PointerEvent is not defined. I use a tsconfig.spec.json (which seems to be loaded by ts-jest) which contains "lib": ...
Read more >
How to Fix ReferenceError: Event is Not Defined in JavaScript
JavaScript's ReferenceError: Event is Not Defined occurs if the event parameter is not declared correctly when using an event handler.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found