jsdom-global
jsdom-global copied to clipboard
Running jsdom with jest
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,
any new thoughts at this issue?
+1
+1
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
@NicoleRauch Do you have the "handwritten JSDOM setup" example?