-
Notifications
You must be signed in to change notification settings - Fork 128
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
Rewiring a module containing DOM manipulation #152
Comments
Hi,
|
The workaround will not work if the imported file performs DOM manipulation at the time it's being loaded rather than inside a function call to be executed later. Granted, by default Jest's native jsdom fails in that case too, but explicitly loading jsdom via Jest's setupFiles flag easily fixes the issue in case of regular "require" but remains broken when using "rewire". |
It's not foolproof, but I came up with a different way to get around this. Basically I duplicated the file I wanted to test, injecting the jsdom directly into the file. I also used my actual HTML in the jsdom. I make no claims of it being a perfect (or perfectly written) solution, but it seems to work.
|
Hi,
I'm trying to use
rewire
from a Jest test. Jest comes withjsdom
anddocument
object is defined inside the test. However, when I try to runconst app = rewire("../app.js")
it fails with the error:
TypeError: Cannot read property 'querySelector' of undefined
when it tries to run the line
const myForm = document.querySelector("#myForm");
When I replace the
rewire
call withrequire
, the file get imported successfully.Is there a way to solve this? Am I doing something wrong? Any help will be appreciated!
The text was updated successfully, but these errors were encountered: