window/document not defined in import
See original GitHub issueHi,
I set up a jest-puppeteer project following the README. In my test file I import a library which works on browser DOM (document.querySelector() …) but I get the following error when running tests:
ReferenceError: document is not defined
So I tried to switch to jsdom by adding the following comments at the top of the file:
/**
* @jest-environment jsdom
*/
But I then get the error:
ReferenceError: page is not defined
How can I do to import my library and still having browser/page puppeteer variables set ?
regards
Issue Analytics
- State:
- Created 4 years ago
- Reactions:6
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Next.js: document is not defined - Stack Overflow
I think, in server rendering mode, the document is undefined. You should be able to use it inside class lifecycle methods or useEffect...
Read more >Solve “document is not defined” errors in Next.js | by WebTutPro
You can import your Scroll component using dynamic imports and the srr: false option. This way your component won't even be rendered on...
Read more >How to solve the document is not defined error - Flavio Copes
Here's how to fix the “referenceerror: document is not defined” error that you might have in Node.js or with a tool like Next.js....
Read more >Fixing Next js "ReferenceError: document is not defined"
Solution 1: Since the window is part of the browser, we can wrap our code inside an if statement. · Solution 2: Since...
Read more >ReferenceError: document is not defined in JavaScript
To fix this error, you need to make sure that your code is not trying to access the document object in a non-browser...
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 FreeTop 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
Top GitHub Comments
I get it work by habving PuppeteerEnvironment extending JsdomEnvironment instead of NodeEnvironment
it is the right way to do it ?
I have to test a library which manipulate the DOM. And I’d like to test the API of this library directly within my puppeteer test file (some test are directly on the library API and some others are on the DOM in browser). But if I use NodeEnvironment my library won’t load due to document not defined error.