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

Can't access the JSDOM API #26

Open
ronami opened this issue May 9, 2017 · 3 comments
Open

Can't access the JSDOM API #26

ronami opened this issue May 9, 2017 · 3 comments

Comments

@ronami
Copy link

ronami commented May 9, 2017

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!

@derwaldgeist
Copy link

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

@Li0liQ
Copy link

Li0liQ commented Jul 16, 2017

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 added a commit to Li0liQ/jsdom-global that referenced this issue Jul 17, 2017
Will allow to access jsdom API (rstacruz#26).
skomorokh added a commit to folio-org/stripes-connect that referenced this issue Jul 27, 2017
I'd have preferred to leave this in place until I've finished building
the replacement. Alas, use of window.alert() didn't play well with the
test environment since jsdom-global doesn't expose enough of jsdom for
me to override window.alert nor prevent its absence from cancelling the
test.

rstacruz/jsdom-global#26
@glebec
Copy link

glebec commented Jun 9, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants