jsdom-global icon indicating copy to clipboard operation
jsdom-global copied to clipboard

Can't access the JSDOM API

Open ronami opened this issue 8 years ago • 3 comments
trafficstars

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!

ronami avatar May 09 '17 15:05 ronami

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

derwaldgeist avatar May 29 '17 00:05 derwaldgeist

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.

Li0liQ avatar Jul 16 '17 08:07 Li0liQ

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.

glebec avatar Jun 09 '19 06:06 glebec