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

Running jsdom with jest #31

Open
frenkelor opened this issue Jun 26, 2017 · 5 comments
Open

Running jsdom with jest #31

frenkelor opened this issue Jun 26, 2017 · 5 comments

Comments

@frenkelor
Copy link

frenkelor commented Jun 26, 2017

I'm getting this error while i'm trying to run jest tests.
node: 7.11.0
npm: 4.2.0
jest: 20.0.4
jsdom: 11.0.0

html-to-json/node_modules/jsdom/lib/jsdom/living/generated/Event.js:231
    throw new TypeError(`${context} is not of type 'Event'.`);
    ^

TypeError: Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'.
    at convert (html-to-json/node_modules/jsdom/lib/jsdom/living/generated/Event.js:231:11)
    at Window.dispatchEvent (html-to-json/node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:88:13)
    at Window.process.nextTick (html-to-json/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/browser/Window.js:492:14)
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)

I'm running require('jsdom-global')() on my setup.js jest file and also inside the file that i using jsdom

I also tried to write my own code and it worked for jest but didn't work from some reason on my production server.

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

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

function copyProps(src, target) {
  const props = Object.getOwnPropertyNames(src)
    .filter(prop => typeof target[prop] === 'undefined')
    .map(prop => Object.getOwnPropertyDescriptor(src, prop));
  Object.defineProperties(target, props);
}

global.window = window;
global.document = window.document;
global.navigator = {
  userAgent: 'node.js',
};
copyProps(window, global);

I will keep investigate this issue just wonted to point that out and see if there is a quick fix for this issue.
thanks,

@raigons
Copy link

raigons commented Aug 4, 2017

any new thoughts at this issue?

@ggrillone
Copy link

+1

1 similar comment
@goodwin64
Copy link

+1

@NicoleRauch
Copy link

Apparently jest already comes with jsdom preconfigured. So if you remove your jsdom-global import (or handwritten JSDOM setup), everything should work, plus the error message should be gone.

For further information see https://stackoverflow.com/questions/40970780/how-to-setup-jsdom-when-working-with-jest

@lsycxyj
Copy link

lsycxyj commented Aug 9, 2018

@NicoleRauch Do you have the "handwritten JSDOM setup" example?

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

6 participants