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.

window/document not defined in import

See original GitHub issue

Hi,

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:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
CreativeBulmacommented, Apr 29, 2019

I get it work by habving PuppeteerEnvironment extending JsdomEnvironment instead of NodeEnvironment

// const NodeEnvironment = require('jest-environment-node');
const JsdomEnvironment = require('jest-environment-jsdom');
const fs = require('fs');
const path = require('path');
const puppeteer = require('puppeteer');
const os = require('os');

const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');

class PuppeteerEnvironment extends JsdomEnvironment {
	constructor(config) {
		super(config);
	}

	async setup() {
		await super.setup();
		// get the wsEndpoint
		const wsEndpoint = fs.readFileSync(path.join(DIR, 'wsEndpoint'), 'utf8');
		if (!wsEndpoint) {
			throw new Error('wsEndpoint not found');
		}

		// connect to puppeteer
		this.global.__BROWSER__ = await puppeteer.connect({
			browserWSEndpoint: wsEndpoint,
		});
	}

	async teardown() {
		await super.teardown();
	}

	runScript(script) {
		return super.runScript(script);
	}
}

module.exports = PuppeteerEnvironment;

it is the right way to do it ?

2reactions
CreativeBulmacommented, Apr 30, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

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