enzyme: Enzyme + jsdom -> TypeError: jsdom is not a function

I am trying to get Enzyme up and running with jsdom as describe here:

http://airbnb.io/enzyme/docs/guides/jsdom.html

/* setup.js */

var jsdom = require('jsdom').jsdom;

global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
  if (typeof global[property] === 'undefined') {
    global[property] = document.defaultView[property];
  }
});

global.navigator = {
  userAgent: 'node.js'
};

However, this gives the following:

TypeError: jsdom is not a function from line 3 global.document = jsdom('');, why? And how to resolve this?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 6
  • Comments: 23 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Got it to work with jsdom@11.1.0 and enzyme@2.9.1 as follows:

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

const { document } = (new JSDOM('')).window;
global.document = document;

I believe you need to make ‘window’ available globally in addition to ‘document’ (shown in the above example). The below works for me:

import jsdom from 'jsdom';
const {JSDOM} = jsdom;
const {document} = (new JSDOM('<!doctype html><html><body></body></html>')).window;
global.document = document;
global.window = document.defaultView;

pls update live docs to v10 😿

edit: fyi unmerged PR docs make everything work fine 🤘