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

Event is missing in in setup file under testEnvironment=node, ReferenceError: Event is not defined, Jest 27 #11528 #11705

Closed
jayphelps opened this issue Jul 30, 2021 · 6 comments · Fixed by #11727

Comments

@jayphelps
Copy link
Contributor

jayphelps commented Jul 30, 2021

🐛 Bug Report

The globals Event and EventTarget are missing in any setup file under testEnvironment=node (the default)

ReferenceError: Event is not defined

#11528 actually caught this but it was closed because presumably it was not known to that maintainer that Node v15.4 got native support for Event and EventTarget.

To Reproduce

Steps to reproduce the behavior:

# 15.4 or higher
nvm use node 15.4
# Optionally confirm that node does in fact have Event natively now
node -e "console.log(Event)"

jest.config.js

module.exports = {
  setupFiles: ['./setupJest.js'],
};

setupJest.js

// use it somehow
Event;
// ReferenceError: Event is not defined

Expected behavior

Event and EventTarget are available globally.

envinfo

System:
    OS: macOS 10.15.6
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
  Binaries:
    Node: 15.7.0 - ~/.nvm/versions/node/v15.7.0/bin/node
    Yarn: 1.22.10 - ~/.nvm/versions/node/v15.0.1/bin/yarn
    npm: 7.4.3 - ~/.nvm/versions/node/v15.7.0/bin/npm
@Smrtnyk
Copy link

Smrtnyk commented Aug 3, 2021

Node 15.7 is experimental and not an lts
Do you have issues with node 14 or node 16?

@jayphelps
Copy link
Contributor Author

@Smrtnyk Unfortunately, yes. Though it's important to note that node 14 does not support Event/EventTarget so it not working there is expected. 15.4 is the minimum that supports Event and EventTarget, and the issue is reproducible from that version and beyond, including node 16.6.1 (latest stable.) I just happened to be using 15.7 but had confirmed that it was not 15.7 specific.

Here's a demo: https://github.com/jayphelps/jest-event-demo

@jayphelps
Copy link
Contributor Author

jayphelps commented Aug 3, 2021

If it's helpful, I did a little bit of digging. I'm not familiar with the jest codebase, but I imagine based on what I can see that it likely creates a new global context and has a hardcoded listed of exposed globals, and Event and EventTarget are just not among them yet because no one has brought it up.

Edit: I have confirmed this is the case. I'll submit a PR to add them. PR available: #11727

https://github.com/facebook/jest/blob/bc50e7f360ab1845abbaa0b3ad788caead0d3174/packages/jest-environment-node/src/index.ts#L34-L68

@Smrtnyk
Copy link

Smrtnyk commented Aug 4, 2021

Makes sense. I see you filed a PR. Have you tried locally with your fix to see if it fixes your issues?

@jayphelps
Copy link
Contributor Author

@Smrtnyk indeed I have. Works great.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants