fczbkk/css-selector-generator

Failed to import it in test environment (node)

mika-cn opened this issue · 2 comments

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)

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.

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.