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: EventTarget is not defined in node<16

See original GitHub issue
  • @testing-library/user-event version: 14.0.0-beta.3

Relevant code or config

new EventTarget()

Problem description:

EventTarget does not exist in older node versions. Importing user-event results in a ReferenceError because: https://github.com/testing-library/user-event/blob/2c5d9f1369301539826879f95c9f6093f676e2bb/src/utils/dataTransfer/Clipboard.ts#L34

Suggested solution:

Either

  • polyfill
  • user-event requires a DOM implementation which requires EventTarget so any DOM implementation should already ship with a polyfill. We might as well just ask our users to set EventTarget globally if they use a DOM implementation that does not already do so in an older node environment.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
make-github-pseudonymous-againcommented, Jan 14, 2022

OK. I may have had too much enthusiasm when the first render-only tests passed. I probably need to do what is explained here for the interactive tests to work. Thanks for taking the time to lead me on the path of solution.

0reactions
make-github-pseudonymous-againcommented, Jan 14, 2022

I managed to make it work using @testing-library/user-event v13.5.0 instead of v14.0.0-beta.x. ~It seems my testing environment already sets up global.window and global.document but not document or window or global.EventTarget.~ Attempting to polyfill only EventTarget produces more errors. Attempting to just import jsdom breaks everything (without even trying to polyfill, I’ve tried it all the way down to v16.5.1).

I found something that works for now…

PS: OK I figured it out. Meteor runs tests both for the server and for the client. The server environment has no window whereas the client environment does have one. I was confused because I was convinced my client test would only run on the client (it was surrounded by if (Meteor.isClient) { ... }. But the import statements are executed on both, and that’s what breaks the server run. So two solutions: dynamic imports inside the client block, or client-only tests. Still deciding on which solution works best.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Event target value is not defined - javascript - Stack Overflow
Just use e.currentTarget.value . I didn't even have to explicitly type e . currentTarget will always be the element you bound the event...
Read more >
Node.js and the struggles of being an EventTarget - NearForm
After the changes described here, the MessagePort class no longer inherits from EventEmitter : Instead, it becomes a subclass of EventTarget ...
Read more >
Errors | Node.js v19.3.0 Documentation
Throws with a ReferenceError because z is not defined. try { const m = 1; ... when an attempt is made to recursively...
Read more >
The complete guide to AbortController in Node.js
Its initial value is undefined . The value of the ... The AbortSignal class extends the EventTarget class and can receive the abort...
Read more >
273731 - EventTarget is not defined - chromium - Monorail
You will receive "ReferenceError: EventTarget is not defined" What is the expected behavior? EventTarget should be defined and Node should ...
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