d3 / d3-selection

Transform the DOM by selecting elements and joining to data.

Home Page:https://d3js.org/d3-selection

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Importing d3-selection causes errors

visusnet opened this issue · comments

The following error message occurs when d3-selection is imported in a test environment before JSDOM (or any other mocked DOM) has initialized its global objects:

Cannot read property 'matches' of undefined
at node_modules/d3-selection/build/d3-selection.js

This is due to the eager initialization of the matcher function. Merely importing d3-selection should not cause problems.

It’s already the case that detection for native element.matches support isn’t run if the global document is not available:

if (typeof document !== "undefined") {

So, it doesn’t feel to me like the eager initialization (in the case a global document is available) is the problem here; it’s something else that’s actually invoking the initialized matcher function, only to find that the element on which you are matching doesn’t support element.matches.

D3 is not a compatibility layer, so I think it’s time to remove the fallback for vendor-prefixed element.matches, and rely on native support for it #187. That eliminates the need for any initialization, though you’ll still get the error above if you’re using an old version of JSDOM or an old browser that doesn’t support element.matches.

(I’ll reopen and investigate further if you can give me a test case to reproduce the error.)