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

Failed to import it in test environment (node) #40

Closed
mika-cn opened this issue Jun 10, 2020 · 2 comments
Closed

Failed to import it in test environment (node) #40

mika-cn opened this issue Jun 10, 2020 · 2 comments
Assignees

Comments

@mika-cn
Copy link

mika-cn commented Jun 10, 2020

import getCssSelector from 'css-selector-generator';

// raise "window is not undefined"

Then I try to import it form source instead from 'build/index.js'.

import getCssSelector from 'css-selector-generator/src/index.js';

// raise "document is not defined"

I use jsdom to provide a browser like environment. But I didn't expose the window or Document object to global context.

Is it possible to access these global variables by the element that passed to getCssSelector(). So there's less global variable dependencies (e.g: using elem.ownerDocument to access Document)

fczbkk added a commit that referenced this issue Jun 11, 2020
When referencing root node, calls to `document` are replaced with calls
to `element.ownerDocument`. This is an attempt to make this library work
in environments with no global objects, e.g. jsdom.

refs #40
@fczbkk fczbkk self-assigned this Jun 11, 2020
@fczbkk
Copy link
Owner

fczbkk commented Jun 11, 2020

This is exactly why I hate virtual environments like jsdom. It's fine for testing simple React components. But it falls apart the moment you are trying to do anything more substantial with DOM. It's a Potemkin village.

@mika-cn I have tried to change the code so that it does not ask directly for document's root node, but for element.ownerDocument's root node. Hope it helps. Please update to v2.1.0 and let me know if it works for you.

@mika-cn
Copy link
Author

mika-cn commented Jun 12, 2020

I agree with you. It's really hard for these library to implement a DOM like environment. I've found some interface's behavior is different from DOM.

I've tried the new version(v2.1.0), it works fine. Nice job and thank you very much.

@fczbkk fczbkk closed this as completed Jun 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants