rstacruz/jsdom-global

Can't access the JSDOM API

Opened this issue · 3 comments

As of version 3, there's no way to access the JSDOM api (here) directly. Some functions such as reconfigure are important.

How do you think we can expose a reference to it? Please guide me and I'll be happy to make a PR.

Thanks!

+1
I also fell in the trap that dom is not exposed and thus dom.reconfigure() won't work.

This is an important issue, because location in jsdom can no longer be changed via defineProperty for test purposes. And the only option available is reconfigure.

I think this manual script from the Enzyme docs more-or-less deprecates this package and solves this issue. Quoting for convenience:

/* setup.js */

const { JSDOM } = require('jsdom');

const jsdom = new JSDOM('<!doctype html><html><body></body></html>');
const { window } = jsdom;

function copyProps(src, target) {
  Object.defineProperties(target, {
    ...Object.getOwnPropertyDescriptors(src),
    ...Object.getOwnPropertyDescriptors(target),
  });
}

global.window = window;
global.document = window.document;
global.navigator = {
  userAgent: 'node.js',
};
global.requestAnimationFrame = function (callback) {
  return setTimeout(callback, 0);
};
global.cancelAnimationFrame = function (id) {
  clearTimeout(id);
};
copyProps(window, global);

You can export the instance, make it global, whatever works for your use case.