ReferenceError: EventTarget is not defined in node<16
See original GitHub issue@testing-library/user-eventversion: 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-eventrequires a DOM implementation which requiresEventTargetso any DOM implementation should already ship with a polyfill. We might as well just ask our users to setEventTargetglobally if they use a DOM implementation that does not already do so in an older node environment.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:8 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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.
I managed to make it work using
@testing-library/user-eventv13.5.0 instead of v14.0.0-beta.x. ~It seems my testing environment already sets upglobal.windowandglobal.documentbut notdocumentorwindoworglobal.EventTarget.~ Attempting to polyfill onlyEventTargetproduces more errors. Attempting to justimportjsdombreaks 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
windowwhereas 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 byif (Meteor.isClient) { ... }. But theimportstatements 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.