question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

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

github_iconTop GitHub Comments

177reactions
vlaurincommented, Jul 12, 2017

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;
32reactions
Mike26372commented, Sep 5, 2017

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;
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: require(...).jsdom is not a function - Stack Overflow
Try using the following, var jsdom = require('jsdom'); const { JSDOM } = jsdom; const { document } = (new JSDOM('')).window; global.document ...
Read more >
Using enzyme with JSDOM - GitHub Pages
Since enzyme's mount API requires a DOM, JSDOM is required in order to use mount if you are not already in a browser...
Read more >
DOM Manipulation - Jest
DOM Manipulation. Another class of functions that is often considered difficult to test is code that directly manipulates the DOM.
Read more >
Setup - Testing Library
React Testing Library does not require any configuration to be used. However, ... You can enable jsdom globally by editing jest.config.js :.
Read more >
Jsdom NPM package broken | Velo by Wix
const jsdom = require("jsdom");. when trying this i get. Uncaught (in promise) TypeError: (0 , _jsdom.require) is not a function.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found